Professional Documents
Culture Documents
Jquery
Jquery
JQuery
jQuery is a lightweight, "write less, do more", JavaScript library.
The purpose of jQuery is to make it much easier to use JavaScript on
your website.
JQuery jQuery takes a lot of common tasks that require many lines of JavaScript code
to accomplish, and wraps them into methods that you can call with a single
line of code.
Dr.V.Mareeswari jQuery also simplifies a lot of the complicated things from JavaScript,
like AJAX calls and DOM manipulation.
Assistant Professsor(Sr)
The jQuery library contains the following features:
School of Information Technology and Engineering HTML/DOM manipulation
VIT,Vellore CSS manipulation
Cabin No:SJT 210-A30 HTML event methods
Effects and animations
AJAX
2 Utilities
Dr. V.Mareeswari / AP(Sr) / SITE / VIT
3 Dr. V.Mareeswari / AP(Sr) / SITE / VIT 4 Dr. V.Mareeswari / AP(Sr) / SITE / VIT
jQuery CDN
jQuery Syntax
If you don't want to download and host jQuery yourself, you can include it
from a CDN (Content Delivery Network). The jQuery syntax is tailor-made for selecting HTML elements and
Both Google and Microsoft host jQuery.
performing some action on the element(s).
Google CDN: Basic syntax is: $(selector).action()
A $ sign to define/access jQuery
<head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jq
uery.min.js"></script></head> A (selector) to "query (or find)" HTML elements
<html><head><script
Event Methods src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.
click()-The function is executed when the user clicks on the HTML
element. js"></script>
dblclick()-The function is executed when the user double-clicks on the <script>
HTML element
$(document).ready(function(){
mouseenter()-The function is executed when the mouse pointer enters
the HTML element $("#p1").mouseenter(function(){
mouseleave()-The function is executed when the mouse pointer leaves the alert("You entered p1!");
HTML element
mousedown()-The function is executed, when the left, middle or right });
mouse button is pressed down, while the mouse is over the HTML element }); </script></head><body>
mouseup()-The function is executed, when the left, middle or right mouse
button is released, while the mouse is over the HTML element <p id="p1">Enter this paragraph.</p></body></html>
hover()-takes two functions and is a combination of the mouseenter() and
mouseleave() methods
focus()-The function is executed when the form field gets focus
blur()-The function is executed when the form field loses focus Demo
on()-method attaches one or more event handlers for the selected elements
14 Dr. V.Mareeswari / AP(Sr) / SITE / VIT
13 Dr. V.Mareeswari / AP(Sr) / SITE / VIT
<html><head><script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">< jQuery Effects - Hide and Show Demo
/script><script> $("#hide").click(function(){
$(document).ready(function(){ $("p").hide();
$("p").on({ });
mouseenter: function(){
$(this).css("background-color", "lightgray"); }, $("#show").click(function(){
mouseleave: function(){ $("p").show();
$(this).css("background-color", "#00FF00"); },
});
click: function(){
$(this).css("background-color", "yellow"); } });}); $("button").click(function(){
</script></head><body><p>Click or move the mouse pointer over this
$("p").hide(1000);
paragraph. <br> I am Prof. Mareeswari V<br>SITE, VIT, });
Vellore.</p></body></html> The optional speed parameter specifies the speed of the hiding/showing,
and can take the following values: "slow", "fast", or milliseconds.
Demo
15 Dr. V.Mareeswari / AP(Sr) / SITE / VIT 16 Dr. V.Mareeswari / AP(Sr) / SITE / VIT
It is also possible to define relative values (the value is then relative to Uses Queue Functionality
the element's current value). This is done by putting += or -= in front This means that if you write multiple animate() calls after each other,
of the value. jQuery creates an "internal" queue with these method calls. Then it
$("button").click(function(){ runs the animate calls ONE by ONE.
$("div").animate({ $("button").click(function(){
left: '250px', var div = $("div");
height: '+=150px', div.animate({height: '300px', opacity: '0.4'}, "slow");
div.animate({width: '300px', opacity: '0.8'}, "slow");
width: '+=150px'
div.animate({height: '100px', opacity: '0.4'}, "slow");
});}); div.animate({width: '100px', opacity: '0.8'}, "slow"); });
You can even specify a property's animation value as "show", "hide", The example below first moves the <div> element to the right, and
or "toggle“ then increases the font size of the text.
$("button").click(function(){ $("button").click(function(){
$("div").animate({ var div = $("div");
height: 'toggle' div.animate({left: '100px'}, "slow");
});}); div.animate({fontSize: '3em'}, "slow"); });
23 Dr. V.Mareeswari / AP(Sr) / SITE / VIT 24 Dr. V.Mareeswari / AP(Sr) / SITE / VIT
Remove Elements
$("#div1").remove();
$("#div1").empty();
Demo
$("p").remove(".test,.demo");
33 Dr. V.Mareeswari / AP(Sr) / SITE / VIT 34 Dr. V.Mareeswari / AP(Sr) / SITE / VIT
Demo
Adding Text
$("p").append(" <b>Appended text</b>.");
$("ol").append("<li>Appended item</li>");
$("p").prepend("<b>Prepended text</b>. ");
$("ol").prepend("<li>Prepended item</li>");
$("img").before("<b>Before</b>");
$("img").after("<i>After</i>");
.append(): This function Insert the
var txt3 = document.createElement("p"); data or content inside an element at last
txt3.innerHTML = "This is paragraph text"; index.
.after(): This function puts the
element after the specified element.
35 Dr. V.Mareeswari / AP(Sr) / SITE / VIT 36 Dr. V.Mareeswari / AP(Sr) / SITE / VIT
map() method
The map() method creates a new array with the results of calling a
function for every array element.
The map() method calls the provided function once for each
element in an array, in order.
Note: map() does not execute the function for array elements
without values.
Note: this method does not change the original array.
jQuery.map( array/object, callback )
array/object: This parameter holds the Array or object to
Demo translate.
callback: This parameter holds the function to process each item
against.
37 Dr. V.Mareeswari / AP(Sr) / SITE / VIT 38 Dr. V.Mareeswari / AP(Sr) / SITE / VIT
EXERCISE
1. Use a jQuery method to hide the <p> element when it is clicked
on.
2. Use a jQuery method to hide the <p> element when it is clicked
on. The speed should be "slow".
3. There is a hidden <p> element in the document. Use a jQuery
method to show the <p> element with a click of a button.
4. Toggle between hiding and showing the <p> element when you
click on the "Toggle" button.
5. Use a jQuery method to slide up the <div> element. The duration
of the effect should be "slow".
41 Dr. V.Mareeswari / AP(Sr) / SITE / VIT 42 Dr. V.Mareeswari / AP(Sr) / SITE / VIT
Exercise Exercise
Develop a quiz application using Jquery methods. The application Use jQuery selectors to identify elements with these
should be developed in such a way that after the user attempted a properties in a hypothetical page:
quiz question, the answer along with the explanation should be All p tags that have no children, but only if they don't have a
displayed when the user clicks the answer button. Use sliding class of ignore
methods to develop the same. Any element with the text "REPLACE_ME" in it.
Develop a animated application using Jquery, the application All div tags with a child that has a class of special
should be developed in such a way that the user pressed the All heading elements (h1, h2, h3, h4, h5, h6)
button, the object along with text should be move from left to Every other visible li. Use the DOM API to target the
right, right to left , top to bottom and bottom to top with #square and periodically change it's position in a random
particular set of intervals after completing the task it should be direction.
hide. Use jQuery selectors instead of the DOM API.
43 Dr. V.Mareeswari / AP(Sr) / SITE / VIT 44 Dr. V.Mareeswari / AP(Sr) / SITE / VIT