JQUERY
JQUERY
• Features of jQuery
$(selector).action()
sign to access jQuery
});
• This is to prevent any jQuery code from
running before the document is finished
loading (is ready).
• We should wait for the document to be fully
loaded and ready before working with it. This
also allows you to have your JavaScript code
before the body of your document, in the
head section.
Or you can simply write:
$(function(){
});
jQuery Selectors
• <h2>This is a heading</h2>
• <p>This is a paragraph.</p>
• <p>This is another paragraph.</p>
• <button>Click me</button>
• </body>
• </html>
Id selector
• <!DOCTYPE html>
• <html>
• <head>
• <script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></
script>
• <script>
• $(document).ready(function(){
• $("button").click(function(){
• $("#test").hide();
• });
• });
• </script>
• </head>
• <body>
• <h2>This is a heading</h2>
• <p>This is a paragraph.</p>
• <p id="test">This is another paragraph.</p>
• <button>Click me</button>
• </body>
• </html>
jQuery Selectors
• Mouse Events:
• click,dblclick,mouseenter,mouseleave
• Keyboard Events:
• keypress,keydown,keyup
• Form Events:
• submit,change,focus,blur
• Document/Window :
• load,resize,scroll,unload
jQuery Syntax For Event Methods
• <p>HELLO WORLD</p>
• <p>Click me away!</p>
• <p>Click me too!</p>
• </body>
• </html>
• dblclick()
The dblclick() method attaches an event
handler function to an HTML element.
The function is executed when the user
double-clicks on the HTML element:
• $("p").dblclick(function(){
$(this).hide();
});
• mouseenter()
• The mouseenter() method attaches an event
handler function to an HTML element.
• The function is executed when the mouse
pointer enters the HTML element:
• $("#p1").mouseenter(function(){
alert("You entered p1!");
});
• mouseleave()
• The mouseleave() method attaches an event
handler function to an HTML element.
The function is executed when the mouse
pointer leaves the HTML element:
$("#p1").mouseleave(function(){
alert("Bye! You now leave p1!");
});
• mouseup()
• The mouseup() method attaches an event
handler function to an HTML element.
• The function is executed, when the left mouse
button is released, while the mouse is over
the HTML element:
• $("#p1").mouseup(function(){
alert("Mouse up over p1!");
});
• mousedown()
• The mousedown() method attaches an event
handler function to an HTML element.
• The function is executed, when the left mouse
button is pressed down, while the mouse is
over the HTML element:
• $("#p1").mousedown(function(){
alert("Mouse down over p1!");
});
• hover()
• The hover() method takes two functions and is
a combination of the mouseenter() and
mouseleave() methods.
• The first function is executed when the mouse
enters the HTML element, and the second
function is executed when the mouse leaves
the HTML element:
Example of hover
• $("#p1").hover(function(){
alert("You entered p1!");
},
function(){
alert(" You now leave p1!");
});
• focus()
• The focus() method attaches an event handler
function to an HTML form field.
• The function is executed when the form field
gets focus:
• $("input").focus(function(){
$(this).css("background-color", "#cccccc");
});
• blur()
• The blur() method attaches an event handler
function to an HTML form field.
• The function is executed when the form field
loses focus:
• $("input").blur(function(){
$(this).css("background-color", "#ffffff");
});
• The on() Method:
• The on() method attaches one or more event
handlers for the selected elements.
• <!DOCTYPE html>
• <html>
• <head>
• <script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.
js"></script>
• <script>
• $(document).ready(function(){
• $("p").on({
• mouseenter: function(){
• $(this).css("background-color", "lightgray");
• },
• mouseleave: function(){
• $(this).css("background-color", "lightblue");
• },
• click: function(){
• $(this).css("background-color", "yellow");
• }
• });
• });
• </script>
• </head>
• <body>
• </body>
• </html>
jQuery Effects - Hide and Show
$(selector).show(speed,callback);
• The optional speed parameter specifies the speed
of the hiding/showing, and can take the following
values: "slow", "fast", or milliseconds.
• The optional callback parameter is a function to
be executed after the hide() or show() method
completes
example
• $("#hide").click(function(){
$("p").hide(); all paragraph elements will be hidden
});
$("#show").click(function(){
$("p").show();
});
jQuery toggle()
• $(document).ready(function(){
• $("button").click(function(){
• $("#div1").fadeIn();
• $("#div2").fadeIn("slow");
• $("#div3").fadeIn(3000);
• }); no of milliseconds
• });
• fadeOut() Method
• The jQuery fadeOut() method is used to fade
out a visible element.
• Syntax:
• $(selector).fadeOut(speed,callback);
• fadeToggle() Method
• The jQuery fadeToggle() method toggles between
the fadeIn() and fadeOut() methods.
• If the elements are faded out, fadeToggle() will
fade them in.
• If the elements are faded in, fadeToggle() will
fade them out.
• Syntax:
• $(selector).fadeToggle(speed,callback);
• fadeTo() Method
• The jQuery fadeTo() method allows fading to a
given opacity (value between 0 and 1).
• Syntax:
• $(selector).fadeTo(speed,opacity,callback)
• The required opacity parameter in the
fadeTo() method specifies fading to a given
opacity (value between 0 and 1).
Sliding Methods
• $("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
});
Set Attributes - attr()
• $("#btn2").click(function(){
• $("ol").append("<li>Appended list item</li>");
• });
• });
• </script>
• </head>
• <body>
• <p>This is a paragraph.</p>
• <p>This is another paragraph.</p>
• <ol>
• <li>List item 1</li>
• <li>List item 2</li>
• <li>List item 3</li>
• </ol>
• </body>
• </html>
Add Several New Elements With
append() and prepend()
• <script>
• function appendText() {
• var txt1 = $("<p></p>").text("orange");
• var txt2 = $("<p></p>").text("apple");
• var txt3 = document.createElement("p");
• txt3.innerHTML = "watermelon";
• $("p").append(txt1, txt2, txt3);
• }
• </script>
• The elements are created with text/HTML,
jQuery, and JavaScript/DOM. Then we append
the new elements to the text with the
append() method (this will work for prepend()
too)
after() and before() Methods
• following methods:
• addClass() - Adds one or more classes to the
selected elements
• removeClass() - Removes one or more classes
from the selected elements
• toggleClass() - Toggles between
adding/removing classes from the selected
elements
• css() - Sets or returns the style attribute
• addClass() Method
• The following example shows how to add class attributes
to different elements. We can select multiple elements,
when adding classes:
• Example
• $("button").click(function(){
$("h1, h2, p").addClass(“ashoka");
$("div").addClass(“raman");
});
• Here we have added a class named as ashoka to 3
elements,h1,h2,p.
• Raman class is added to div element.
• We can also specify multiple classes within the
addClass() method.
• removeClass() Method
• The following example shows how to remove
a specific class attribute from different
elements:
• Example
• $("button").click(function(){
$("h1, h2, p").removeClass(“ashoka");
});
• ashoka class has been removed from h1,h2,p
• toggleClass() Method
• The following example will show how to use
the jQuery toggleClass() method. This method
toggles between adding/removing classes
from the selected elements:
• Example
• $("button").click(function(){
$("h1, h2, p").toggleClass("blue");
});
• css() Method
• The css() method sets or returns one or more
style properties for the selected elements.
• Return a CSS Property
• To return the value of a specified CSS property,
use the following syntax:
• css("propertyname");
• The following example will return the
background-color value of the FIRST matched
paragraph element:
• Example
• $("p").css("background-color");
• Set a CSS Property
• To set a specified CSS property, use the following
syntax:
• css("propertyname","value");
• The following example will set the background-
color value for ALL matched elements:
• Example
• $("p").css("background-color", "yellow");
• Set Multiple CSS Properties
• To set multiple CSS properties, use the following
syntax:
• css({"propertyname":"value","propertyname":"va
lue",...});
• The following example will set a background-
color and a font-size for ALL matched elements:
• Example
• $("p").css({"background-color": "yellow", "font-
size": "200%"});
Dimension Methods