jQuery traversing

You can traverse through the elements in the order they are combined. All the elements are mostly combined in the form of a tree, and we can traverse them starting from the root.

Note

Elements are not combined per se, but they are rather structured or modeled within the document object.

Let's take a look at the following image:

jQuery traversing

Let's take a look at the following description:

  • The <div> element is the parent of <ul> and an ancestor of everything inside it
  • The <ul> element is the parent of both the <li> elements and a child of <div>
  • The left <li> element is the parent of <span>, child of <ul>, and a descendant of <div>
  • The <span> element is a child of the left <li> and a descendant of <ul> and <div>
  • The two <li> elements are siblings (they share the same parent)
  • The right <li> element is the parent of <b>, child of <ul>, and a descendant of <div>
  • The <b> element is a child of the right <li> and a descendant of <ul> and <div>

Ancestors

An ancestor is a parent!

We will cover three useful jQuery methods for traversing up the DOM tree in the following sections.

parent()

The parent() function returns the parent of selected element.

Parameters

This takes no parameter, but it is called as a function of the jQuery object.

Returns

This returns the parent of the element.

Description

This function returns the parent of the selected element:

$(document).ready(function() {
  $("span").parent();
});

parents():

The parents(): function returns an array of parents of the selected element till root.

Parameters

This takes no parameter but is called as a function of the jQuery object.

Returns

This function returns all the parents of an element.

Description

The parents(): function returns an array of parents of the selected element till root:

$(document).ready(function() {
  $("span").parents();
});

parentsUntil():

$(selector).parentsUntil(stop,filter);

The parentsUntil() method returns all ancestors between the selector and stop element.

An ancestor element is a parent, grandparent, great-grandparent, and so on.

Parameters

Stop an optional parameter that indicates where to stop the search for matching ancestor elements. Filter an optional parameter usually an expression to narrow down the search between selector and stop.

Returns

This returns all parents between the two selected elements.

Description

This function returns the parents between two selected elements:

$(document).ready(function() {
  $("span").parentsUntil("div");
});

Descendants

A child of the parent is called its descendant.

In the following sections, we will cover two useful jQuery methods for traversing down the DOM tree.

children()

The children() function returns the children of the selected element.

Parameters

This takes no parameters but it is called as a function of the jQuery object.

Returns

This returns an array of HTML elements that are children of the target element.

Description

This function returns the children of the selected element:

$(document).ready(function(){
  $("div").children();
});

find()

The find() function returns an array of children up to the child that has no children of its own, which is also called a leaf.

Parameters

The find() function takes the HTML element as parameters.

Returns

This returns all the children of the target element.

Description

This function returns an array of children up to the leaf of the tree:

$(document).ready(function() {
  $("div").find("span");
});

Siblings

Siblings are elements that share a same parent.

There are many useful jQuery methods for traversing sideways in the DOM tree, which we cover here.

siblings()

$(selector).siblings(filter);

The siblings() function returns all the siblings of the selected element.

Parameters

Filter is an optional parameter usually an expression to narrow down the search among all siblings.

Returns

This returns all the siblings of the element.

Description

This function returns all the siblings of the selected element using the filter parameter:

$(document).ready(function() {
  $("span").siblings("div");
});

next()

$(selector).next(filter);

Filter is an optional parameter usually an expression to narrow down the search for next sibling.

Parameters

This takes no parameters, but is called as a function of the jQuery object.

Returns

This returns the next siblings of the element.

Description

This function returns the next siblings of the selected element using the filter parameter:

$(document).ready(function(){
  $("span").next();
});

nextAll()

$(selector).nextAll(filter);

The nextAll() function returns an array of the next siblings of the selected element.

Parameters

Filter is an optional parameter usually an expression to narrow down the search for all next sibling.

Returns

This returns an array of all the next siblings of the target element.

Description

This function returns an array of the next siblings of the selected element using the parameter:

$(document).ready(function() {
  $("span").nextAll();
});

nextUntil()

$(selector).nextUntil(stop, filter);

The nextUntil() function returns an array of next siblings of the selected element between two specified elements.

Parameters

Stop an optional parameter that indicates where to stop the search for next matching sibling elements.

Filter an optional parameter usually an expression to narrow down the search for sibling elements between selector and stop.

Returns

This returns all the next siblings of the element.

Description

This function returns an array of next siblings of the selected element between two elements:

$(document).ready(function() {
  $("span").nextUntil('H4');
});

prev()

$(selector).prev(filter);

The prev() function returns the previous siblings of the selected element.

Parameters

Filter is an optional parameter usually an expression to narrow down the search for previous sibling.

Returns

The prev() function returns the previous siblings of the element.

Description

This function returns the previous siblings of the selected element using the filter parameter:

$(document).ready(function() {
  $("span").prev();
});

prevAll()

$(selector).prevAll(filter);

The prevAll() function returns an array of the previous siblings of the selected element.

Parameters

Filter is an optional parameter usually an expression to narrow down the search for all previous siblings.

Returns

The prevAll() function returns the array of all the previous siblings of the element.

Description

This function returns an array of the previous siblings of the selected element using the parameter:

$(document).ready(function() {
  $("span").prevAll();
});

prevUntil()

The prevUntil() function returns an array of the previous siblings of the selected element between two elements.

Parameters

This takes the element up to which the search of siblings is limited.

Returns

This returns an array of all the previous siblings of the element.

Description

This function returns an array of the previous siblings of the selected element and the parameter:

$(document).ready(function() {
  $("span").prevUntil("Div");
});

Filtering

The filtering method is used to locate a specific element based on its location.

first()

The first() function outputs the first element of the selected elements.

Parameters

The first() function takes the selected element as parameter.

Returns

This returns a jQuery object that stores a reference to the first item from an array of items matching the provided selector string.

Description

This function outputs the first element of the selected elements. The following example outputs the first H1 heading inside the <div> tag:

$(document).ready(function() {
  $("div H1").first();
});

last()

This function returns the last element of the selected elements.

Parameters

The last() function takes the selected element as parameter.

Returns

This returns a jQuery object that stores a reference to the last item from an array of items matching the provided selector string.

Description

This function outputs the last element of the selected elements. The following example outputs the last H1 heading inside the <div> tag:

$(document).ready(function() {
  $("div H1").last();
});

eq()

The eq() function returns the element specified at the corresponding index number, provided that we begin the numbering from 0. Hence, the first element will have its index number as 0, the second element will have the index number 1, and so on.

Parameters

The eq() function takes the selected element and the index number as parameters.

Returns

This returns the element at the specified index number.

Description

This function returns the element specified at the corresponding index number. The following example returns the fifth div element:

$(document).ready(function() {
 $("div").eq(4);
});

filter()

The filter() function is used to obtain a list of elements that satisfy a particular condition. All the elements that satisfy the specified condition will be returned.

Parameters

The element to be searched and the condition that the element must satisfy are taken as parameters here.

Returns

This returns a list of elements that satisfy a specified condition.

Description

The filter() function is useful for searching and obtaining a list of elements that satisfy a specified condition. In the following example, we will search and obtain a list of all the <div> elements that have their class named as Feedback:

$(document).ready(function() {
  $("div").filter(".Feedback");
});

Note

The not() method is the reverse of the filter() method. If you want to find elements that do not satisfy the mentioned condition, not() can be used.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.93.20