Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

jQuery

Uploaded by

Dewang Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

jQuery

Uploaded by

Dewang Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

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 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.
• jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls
and DOM manipulation.
• The jQuery library contains the following features:
• HTML/DOM manipulation
• CSS manipulation
• HTML event methods
• Effects and animations
• AJAX
• Utilities
Adding jQuery to Your Web Pages
1. Download the jQuery library from jQuery.com
2. Include jQuery from a CDN, like Google
Downloading jQuery
• The jQuery library is a single JavaScript file, and you reference it with the HTML <script> tag (notice that
the <script> tag should be inside the <head> section):
<head>
<script src="jquery-3.4.1.min.js"></script>
</head>
jQuery CDN
• If you don't want to download and host jQuery yourself, you can include it from a CDN (Content Delivery
Network).
• Both Google and Microsoft host jQuery.
• To use jQuery from Google or Microsoft, use one of the following:
Google CDN:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
Microsoft CDN:
<head>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.min.js"></script>
</head>
jQuery Syntax
$(selector).action()
• A $ sign to define/access jQuery
• A (selector) to "query (or find)" HTML elements
• A jQuery action() to be performed on the element(s)
Example:
$(this).hide() - hides the current element.
$("p").hide() - hides all <p> elements.
$(".test").hide() - hides all elements with class="test".
$("#test").hide() - hides the element with id="test".
The Document Ready Event
• All jQuery methods are inside a document ready event:

$(document).ready(function(){
// jQuery methods go here...
});

• This is to prevent any jQuery code from running before the document is
finished loading (is ready).
• some examples of actions that can fail if methods are run before the
document is fully loaded:
• Trying to hide an element that is not created yet
• Trying to get the size of an image that is not loaded yet
jQuery Selectors
• jQuery selectors allow you to select and manipulate HTML element(s).
• jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes,
types, attributes, values of attributes and much more.
• All selectors in jQuery start with the dollar sign and parentheses: $().
The element Selector
• The jQuery element selector selects elements based on the element name.
• You can select all <p> elements on a page like this: $("p")
• When a user clicks on a button, all <p> elements will be hidden:

$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
The #id Selector
• The jQuery #id selector uses the id attribute of an HTML tag to find
the specific element.
• An id should be unique within a page, so you should use the #id
selector when you want to find a single, unique element.
• To find an element with a specific id, write a hash character, followed
by the id of the HTML element: $("#test")
• When a user clicks on a button, the element with id="test" will be
hidden:
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
The .class Selector
• The jQuery .class selector finds elements with a specific class.
• To find elements with a specific class, write a period character,
followed by the name of the class: $(".test")
• When a user clicks on a button, the elements with class="test" will be
hidden:
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
jQuery Selectors
• $("*") ------->Selects all elements
• $(this) ------>Selects the current HTML element
• $("p.intro")-->Selects all <p> elements with class="intro"
• $("p:first")--->Selects the first <p> element
• $("ul li:first")------->Selects the first <li> element of the first <ul>
• $("ul li:first-child")-->Selects the first <li> element of every <ul>
• $("[href]")--->Selects all elements with an href attribute
• $("a[target='_blank']")---->Selects all <a> elements with a target attribute value equal
to "_blank"
• $("a[target!='_blank']")---->Selects all <a> elements with a target attribute value NOT
equal to "_blank"
• $(":button")-->Selects all <button> elements and <input> elements of type="button"
• $("tr:even")-->Selects all even <tr> elements
• $("tr:odd")---->Selects all odd <tr> elements
jQuery Event Methods
• moving a mouse over an element
• selecting a radio button
• clicking on an element
Mouse Event:- click,dbclick,mouseenter,mouseleave
Keyboard Event:- keypress,keyup,keydown
Form event:- submit,change,focus,blur
Document/ Window event:- load,unload,resize,scroll
jQuery Syntax For Event Methods
• To assign a click event to all paragraphs on a page, you can do this:
$("p").click();
• The next step is to define what should happen when the event fires. You must pass a function to
the event:
$("p").click(function(){
// action goes here!!
});
Commonly Used jQuery Event Methods
$(document).ready()
• The $(document).ready() method allows us to execute a function when the document is fully loaded.
click()
• The function is executed when the user clicks on the HTML element.
• The following example says: When a click event fires on a <p> element; hide the current <p> element:
$("p").click(function(){
$(this).hide();
});
dblclick()
• The function is executed when the user double-clicks on the HTML element:
$("p").dblclick(function(){
$(this).hide();
});
mouseenter()
• The function is executed when the mouse pointer enters the HTML element:
$("#p1").mouseenter(function(){
alert("You entered p1!");
});
mouseleave()
• The function is executed when the mouse pointer leaves the HTML element:
$("#p1").mouseleave(function(){
alert("Bye! You now leave p1!");
});
mousedown()
• The function is executed, when the left, middle or right mouse button is
pressed down, while the mouse is over the HTML element:
$("#p1").mousedown(function(){
alert("Mouse down over p1!");
});
mouseup()
• The function is executed, when the left, middle or right mouse button is released, while the mouse is over the HTML
element:
$("#p1").mouseup(function(){
alert("Mouse up 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:
$("#p1").hover(function(){
alert("You entered p1!");
},
function(){
alert("Bye! You now leave p1!");
});
focus()
• The function is executed when the form field gets focus:
$("input").focus(function(){
$(this).css("background-color", "#cccccc");
});
blur()
• The function is executed when the form field loses focus:
$("input").blur(function(){
$(this).css("background-color", "#ffffff");
});
on() Method
• The on() method attaches one or more event handlers for the
selected elements.
• Attach a click event to a <p> element:
$("p").on("click", function(){
$(this).hide();
});
jQuery Effects
jQuery hide() and show()
• With jQuery, you can hide and show HTML elements with the hide() and show() methods:
$("#hide").click(function(){
$("p").hide();
});

$("#show").click(function(){
$("p").show();
});
Syntax:
$(selector).hide(speed,callback);
$(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
$("button").click(function(){
$("p").hide(1000);
});
jQuery toggle()
• You can also toggle between hiding and showing an element with the
toggle() method.
• Shown elements are hidden and hidden elements are shown:
$("button").click(function(){
$("p").toggle();
});
Syntax:
$(selector).toggle(speed,callback);
• The optional speed parameter can take the following values: "slow",
"fast", or milliseconds.
• The optional callback parameter is a function to be executed after
toggle() completes.
jQuery Effects - Fading
• With jQuery you can fade elements in and out of visibility.
• jQuery has the following fade methods:
• fadeIn()
• fadeOut()
• fadeToggle()
• fadeTo()
jQuery fadeIn() Method
• The jQuery fadeIn() method is used to fade in a hidden element.
• Syntax:
$(selector).fadeIn(speed,callback);
• The optional speed parameter specifies the duration of the effect. It can take the following
values: "slow", "fast", or milliseconds.
• The optional callback parameter is a function to be executed after the fading completes.
Example
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
jQuery fadeOut() Method
• The jQuery fadeOut() method is used to fade out a visible element.
• Syntax:
$(selector).fadeOut(speed,callback);
• The optional speed parameter specifies the duration of the effect. It
can take the following values: "slow", "fast", or milliseconds.
• The optional callback parameter is a function to be executed after the
fading completes.
Example
$("button").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});
jQuery 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);
• The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
• The optional callback parameter is a function to be executed after the fading
completes.
Example
$("button").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});
jQuery 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 speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
• The required opacity parameter in the fadeTo() method specifies fading to a
given opacity (value between 0 and 1).
• The optional callback parameter is a function to be executed after the function
completes.
Example
$("button").click(function(){
$("#div1").fadeTo("slow", 0.15);
$("#div2").fadeTo("slow", 0.4);
$("#div3").fadeTo("slow", 0.7);
});
jQuery Effects - Sliding
• The jQuery slide methods slide elements up and down.
• jQuery has the following slide methods:
• slideDown()
• slideUp()
• slideToggle()
jQuery slideDown() Method
• The jQuery slideDown() method is used to slide down an element.
• Syntax:
$(selector).slideDown(speed,callback);
• The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
• The optional callback parameter is a function to be executed after the sliding
completes.
Example
$("#flip").click(function(){
$("#panel").slideDown();
jQuery slideUp() Method
• The jQuery slideUp() method is used to slide up an element.
• Syntax:
$(selector).slideUp(speed,callback);
• The optional speed parameter specifies the duration of the effect. It
can take the following values: "slow", "fast", or milliseconds.
• The optional callback parameter is a function to be executed after the
sliding completes.
Example
$("#flip").click(function(){
$("#panel").slideUp();
});
jQuery slideToggle() Method
• The jQuery slideToggle() method toggles between the slideDown() and
slideUp() methods.
• If the elements have been slide down, slideToggle() will slide them up.
• If the elements have been slide up, slideToggle() will slide them down.
$(selector).slideToggle(speed,callback);
• The optional speed parameter can take the following values: "slow",
"fast", milliseconds.
• The optional callback parameter is a function to be executed after the
sliding completes.
Example
$("#flip").click(function(){
$("#panel").slideToggle();
});
jQuery Effects - Animation
• With jQuery, you can create custom animations.
The animate() Method
• The jQuery animate() method is used to create custom animations.
• Syntax:
$(selector).animate({params},speed,callback);
• The required params parameter defines the CSS properties to be animated.
• The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
• The optional callback parameter is a function to be executed after the animation
completes.
• The following example moves a <div> element to the right, until it has reached a left
property of 250px:
Example
$("button").click(function(){
$("div").animate({left: '250px'});
});
jQuery animate() - Manipulate Multiple Properties
• Notice that multiple properties can be animated at the same time
$("button").click(function(){
$("div").animate({
left: '250px',
opacity: '0.5',
height: '150px',
width: '150px'
});
});
jQuery animate() - Using Relative Values
• It is also possible to define relative values (the value is then relative to the element's current
value). This is done by putting += or -= in front of the value:
$("button").click(function(){
$("div").animate({
left: '250px',
height: '+=150px',
width: '+=150px'
});
});
jQuery animate() - Using Pre-defined Values
• You can even specify a property's animation value as "show", "hide", or "toggle":
$("button").click(function(){
$("div").animate({
height: 'toggle'
});
});
jQuery animate() - Uses Queue Functionality
• By default, jQuery comes with queue functionality for animations.
• This means that if you write multiple animate() calls after each other, jQuery creates an "internal" queue
with these method calls. Then it runs the animate calls ONE by ONE.
• So, if you want to perform different animations after each other, we take advantage of the queue
functionality:
$("button").click(function(){
var div = $("div");
div.animate({height: '300px', opacity: '0.4'}, "slow");
div.animate({width: '300px', opacity: '0.8'}, "slow");
div.animate({height: '100px', opacity: '0.4'}, "slow");
div.animate({width: '100px', opacity: '0.8'}, "slow");
});
jQuery Stop Animations
• The jQuery stop() method is used to stop animations or effects before it is finished.
• The stop() method works for all jQuery effect functions, including sliding, fading and custom
animations.
• Syntax:
$(selector).stop(stopAll,goToEnd);
• The optional stopAll parameter specifies whether also the animation queue should be cleared or not.
Default is false, which means that only the active animation will be stopped, allowing any queued
animations to be performed afterwards.
• The optional goToEnd parameter specifies whether or not to complete the current animation
immediately. Default is false.
• So, by default, the stop() method kills the current animation being performed on the selected
element.
• The following example demonstrates the stop() method, with no parameters:
Example
$("#stop").click(function(){
$("#panel").stop();
});
jQuery Callback Functions
• A callback function is executed after the current effect is 100% finished.
• JavaScript statements are executed line by line. However, with effects, the
next line of code can be run even though the effect is not finished. This
can create errors.
• To prevent this, you can create a callback function.
• A callback function is executed after the current effect is finished.
$(selector).hide(speed,callback);
Example:-
$("button").click(function(){
$("p").hide("slow", function(){ Without callback()
alert("The paragraph is now hidden"); $("button").click(function(){
$("p").hide(1000);
}); alert("The paragraph is now hidden");
}); });
jQuery - Chaining
• With jQuery, you can chain together actions/methods.
• Chaining allows us to run multiple jQuery methods (on the same element) within a
single statement.
• Until now we have been writing jQuery statements one at a time (one after the
other).
• However, there is a technique called chaining, that allows us to run multiple jQuery
commands, one after the other, on the same element(s).
• To chain an action, you simply append the action to the previous action.
• The following example chains together the css(), slideUp(), and slideDown() methods.
The "p1" element first changes to red, then it slides up, and then it slides down:
$("#p1").css("color", "red").slideUp(2000).slideDown(2000);
jQuery - Get Content and Attributes
• Three simple, but useful, jQuery methods for DOM manipulation are:
• text() - Sets or returns the text content of selected elements
• html() - Sets or returns the content of selected elements (including HTML
markup)
• val() - Sets or returns the value of form fields
•Example
$("#btn1").click(function(){
alert("Text: " + $("#test").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});
$("#btn1").click(function(){
alert("Value: " + $("#test").val());
});
Get Attributes - attr()
• The jQuery attr() method is used to get attribute values.
• The following example demonstrates how to get the value of the href attribute in a link:
• Example
• $("button").click(function(){
• alert($("#w3s").attr("href"));
• });
jQuery - Set Content and Attributes
• Set Content - text(), html(), and val()
• text() - Sets or returns the text content of selected elements
• html() - Sets or returns the content of selected elements (including HTML markup)
• val() - Sets or returns the value of form fields
• Example
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
});
A Callback Function for text(), html(), and val()
• The callback function has two parameters: the index of the current element in the list
of elements selected and the original (old) value.
• You then return the string you wish to use as the new value from the function.
$("#btn1").click(function(){
$("#test1").text(function(i, origText){
return "Old text: " + origText + " New text: Hello world!
(index: " + i + ")";
});
});

$("#btn2").click(function(){
$("#test2").html(function(i, origText){
return "Old html: " + origText + " New html: Hello <b>world!</b>
(index: " + i + ")";
});
});
Set Attributes - attr()
• The jQuery attr() method is also used to set/change attribute values.
Example
$("button").click(function(){
$("#w3s").attr("href", "https://www.w3schools.com/jquery/");
});
• The attr() method also allows you to set multiple attributes at the
same time.
Example
$("button").click(function(){
$("#w3s").attr({
"href" : "https://www.w3schools.com/jquery/",
"title" : "W3Schools jQuery Tutorial"
});
});
A Callback Function for attr()
• The callback function has two parameters: the index of the current
element in the list of elements selected and the original (old) attribute
value.
• You then return the string you wish to use as the new attribute value
from the function.
Example
$("button").click(function(){
$("#w3s").attr("href", function(i, origValue){
return origValue + "/jquery/";
});
});
jQuery - Add Elements
Add New HTML Content
• Four jQuery methods that are used to add new content:
• append() - Inserts content at the end of the selected elements
• prepend() - Inserts content at the beginning of the selected elements
• after() - Inserts content after the selected elements
• before() - Inserts content before the selected elements
jQuery append() Method
•The jQuery append() method inserts content AT THE END of the selected HTML elements
Example
$("p").append("Some appended text.");

jQuery prepend() Method


•The jQuery prepend() method inserts content AT THE BEGINNING of the selected HTML
elements.
Example
$("p").prepend("Some prepended text.");
jQuery after() and before() Methods
• The jQuery after() method inserts content AFTER the selected HTML elements.
• The jQuery before() method inserts content BEFORE the selected HTML elements.
Example
$("img").after("Some text after");
$("img").before("Some text before");
jQuery - Remove Elements
• To remove elements and content, there are mainly two jQuery methods:
• remove() - Removes the selected element (and its child elements)
• empty() - Removes the child elements from the selected element
jQuery remove() Method
• The jQuery remove() method removes the selected element(s) and its child elements.
Example
$("#div1").remove();
jQuery empty() Method
• The jQuery empty() method removes the child elements of the selected element(s).
Example
$("#div1").empty();
Filter the Elements to be Removed
• The jQuery remove() method also accepts one parameter, which allows
you to filter the elements to be removed.
• The parameter can be any of the jQuery selector syntaxes.
• The following example removes all <p> elements with class="test":
Example
$("p").remove(".test");
• This example removes all <p> elements with class="test" or
class="demo":
Example
$("p").remove(".test, .demo");
jQuery - Get and Set CSS Classes
• jQuery has several methods for CSS manipulation. We will look at the 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
jQuery addClass() Method
$("button").click(function(){
$("h1, h2, p").addClass("blue");
$("div").addClass("important");
});
jQuery removeClass() Method
$("button").click(function(){
$("h1, h2, p").removeClass("blue");
});
jQuery toggleClass() Method
• This method toggles between adding/removing classes from the selected elements:
Example
$("button").click(function(){
$("h1, h2, p").toggleClass("blue");
});
jQuery css() Method
• The css() method sets or returns one or more style properties for the selected elements.
• 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
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":"value",...});
• 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%"});
jQuery - Dimensions
jQuery Dimension Methods
• jQuery has several important methods for
working with dimensions:
• width()
• height()
• innerWidth()
• innerHeight()
• outerWidth()
• outerHeight()
jQuery width() and height() Methods
• The width() method sets or returns the width of
an element (excludes padding, border and
margin).
• The height() method sets or returns the height
of an element (excludes padding, border and
margin).
• The following example returns the width and height of a specified <div> element:
Example
$("button").click(function(){
var txt = "";
txt += "Width: " + $("#div1").width() + "</br>";
txt += "Height: " + $("#div1").height();
$("#div1").html(txt);
});
jQuery innerWidth() and innerHeight() Methods
• The innerWidth() method returns the width of an element (includes padding).
• The innerHeight() method returns the height of an element (includes padding).
• The following example returns the inner-width/height of a specified <div> element:
Example
$("button").click(function(){
var txt = "";
txt += "Inner width: " + $("#div1").innerWidth() + "</br>";
txt += "Inner height: " + $("#div1").innerHeight();
$("#div1").html(txt);
});
jQuery outerWidth() and outerHeight() Methods
• The outerWidth() method returns the width of an element (includes padding and border).
• The outerHeight() method returns the height of an element (includes padding and border).
Example
$("button").click(function(){
var txt = "";
txt += "Outer width: " + $("#div1").outerWidth() + "</br>";
txt += "Outer height: " + $("#div1").outerHeight();
$("#div1").html(txt);
});
• The outerWidth(true) method returns the width of an element (includes padding, border, and margin).
• The outerHeight(true) method returns the height of an element (includes padding, border, and margin).
Example
$("button").click(function(){
var txt = "";
txt += "Outer width (+margin): " + $("#div1").outerWidth(true) + "</br>";
txt += "Outer height (+margin): " + $("#div1").outerHeight(true);
$("#div1").html(txt);
});
jQuery Traversing
• jQuery traversing, which means "move through", are used to "find" (or
select) HTML elements based on their relation to other elements.
• Start with one selection and move through that selection until you reach
the elements you desire.
• With jQuery traversing, you can easily move up (ancestors), down
(descendants) and sideways (siblings) in the tree, starting from the
selected (current) element. This movement is called traversing - or
moving through - the DOM tree.
jQuery Traversing - Ancestors
• With jQuery you can traverse up the DOM tree to find ancestors of an element.
• An ancestor is a parent, grandparent, great-grandparent, and so on.
• Three useful jQuery methods for traversing up the DOM tree are:
• parent()
• parents()
• parentsUntil()
jQuery parent() Method
• The parent() method returns the direct parent element of the selected element.
• This method only traverse a single level up the DOM tree.
• The following example returns the direct parent element of each <span> elements:
Example
$(document).ready(function(){
$("span").parent();
});
jQuery parents() Method
• The parents() method returns all ancestor elements of the selected
element, all the way up to the document's root element (<html>).
• The following example returns all ancestors of all <span> elements:
Example
$(document).ready(function(){
$("span").parents();
});
• You can also use an optional parameter to filter the search for ancestors.
• The following example returns all ancestors of all <span> elements that
are <ul> elements:
Example
$(document).ready(function(){
$("span").parents("ul");
});
jQuery parentsUntil() Method
• The parentsUntil() method returns all ancestor elements between
two given arguments.
• The following example returns all ancestor elements between a
<span> and a <div> element:
Example
$(document).ready(function(){
$("span").parentsUntil("div");
});
jQuery Traversing - Descendants
• With jQuery you can traverse down the DOM tree to find descendants of an element.
• A descendant is a child, grandchild, great-grandchild, and so on.
• Two useful jQuery methods for traversing down the DOM tree are:
• children()
• find()
jQuery children() Method
• The children() method returns all direct children of the selected element.
• This method only traverses a single level down the DOM tree.
• The following example returns all elements that are direct children of each <div> elements:
Example
$(document).ready(function(){
$("div").children();
});
• You can also use an optional parameter to filter the search for children.
• The following example returns all <p> elements with the class name "first", that are direct children of <div>:
Example
$(document).ready(function(){
$("div").children("p.first");
});
jQuery find() Method
• The find() method returns descendant elements of the selected
element, all the way down to the last descendant.
• The following example returns all <span> elements that are
descendants of <div>:
Example
$(document).ready(function(){
$("div").find("span");
});
• The following example returns all descendants of <div>:
Example
$(document).ready(function(){
$("div").find("*");
});
jQuery Traversing - Siblings
• There are many useful jQuery methods for traversing sideways in the DOM
tree:
• siblings()
• next()
• nextAll()
• nextUntil()
• prev()
• prevAll()
• prevUntil()
jQuery siblings() Method
• The siblings() method returns all sibling elements of the selected element.
Example
$(document).ready(function(){
$("h2").siblings();
});
• You can also use an optional parameter to filter the search for siblings.
• The following example returns all sibling elements of <h2> that are <p> elements:
Example
$(document).ready(function(){
$("h2").siblings("p");
});
jQuery next() Method
• The next() method returns the next sibling element of the selected element.
• The following example returns the next sibling of <h2>:
Example
$(document).ready(function(){
$("h2").next();
});
jQuery nextAll() Method
• The nextAll() method returns all next sibling elements of the selected element.
• The following example returns all next sibling elements of <h2>:
Example
$(document).ready(function(){
$("h2").nextAll();
});
jQuery nextUntil() Method
• The nextUntil() method returns all next sibling elements between two
given arguments.
• The following example returns all sibling elements between a <h2>
and a <h6> element:
Example
$(document).ready(function(){
$("h2").nextUntil("h6");
});
jQuery Traversing - Filtering
The first(), last(), eq(), filter() and not() Methods
• The most basic filtering methods are first(), last() and eq(), which
allow you to select a specific element based on its position in a group
of elements.
• Other filtering methods, like filter() and not() allow you to select
elements that match, or do not match
jQuery first() Method
• The first() method returns the first element of the specified elements.
• The following example selects the first <div> element:
Example
$(document).ready(function(){
$("div").first();
});
jQuery last() Method
• The last() method returns the last element of the specified elements.
• The following example selects the last <div> element:
Example
$(document).ready(function(){
$("div").last();
});
jQuery eq() method
• The eq() method returns an element with a specific index number of the selected
elements.
• The index numbers start at 0, so the first element will have the index number 0
and not 1. The following example selects the second <p> element (index number
1):
Example
$(document).ready(function(){
$("p").eq(1);
});
jQuery filter() Method
• The filter() method lets you specify a criteria. Elements that do not match the
criteria are removed from the selection, and those that match will be returned.
• The following example returns all <p> elements with class name "intro":
Example
$(document).ready(function(){
$("p").filter(".intro");
});
jQuery not() Method
• The not() method returns all elements that do not match the criteria.
• Tip: The not() method is the opposite of filter().
• The following example returns all <p> elements that do not have class name
"intro":
Example
$(document).ready(function(){
$("p").not(".intro");
});

You might also like