jQueryTraversing Data Object Model

Traversing DOM

Various DOM traversal methods are provided to help us select the elements in a document either randomly or sequentially.

Following are some of the DOM traversing methods used to filter out the various elements from a list of DOM elements:

SelectorDescription
eq(index)The set of matched elements to a single element is reduced.
filter(selector)All the elements from the set of matched elements that do not match the specified selector are removed.
filter (function)All the elements from the set of matched elements that do not match the specified function are removed.
is(selector)The current selection is checked against an expression and returns true, if atleast one element of the selection fits the given selector.
map (callback)A set of elements in the jQuery object into another set of values in a jQuery array is converted.
not(selector)The elements matching the specified selector from the set of matched elements are removed.
slice(start, end)A subset of the matched elements are selected.

Following are some of the DOM traversing methods used to REFINE(LOCATE or FIND) various DOM elements:

SelectorDescription
add(selector)Adds more elements matched by the given selector to the set of matched elements.
andSelf()The previous selection is added to the current selection.
children([selector])A set of elements is found which contains all the unique immediate children of each of the matched set of elements.
closest(selector)A set of elements is found which contains the closest parent element that matches the specified selector including the starting element.
end()The most recent 'destructive' operation is reverted changing the set of the matched elements to its previous state.
contents()All the child nodes inside the matched elements can be found.
find(selector)Searches the elements matching the specified selectors.
next(selector)Finds a set of elements that contains the unique next siblings of each of the given set of elements.
nextAll(selector)Finds the sibling elements after the current element.
offsetParent()Returns a jQuery collection with the positioned parent of the first matched element.
parent(selector)Gets the direct parent of an element. If it calls a set of elements the parent returns a set of their unique direct parent elements.
parents(selector)Finds a set of elements containing the unique ancestors of the matched set of elements (except for the root element).
prev(selector)A set of elements is found that contains the unique previous siblings of each of the matched set of the elements.
prevAll(selector)It finds all the sibling elements in front of the current element.
siblings(selector)It finds a set of elements that contain all the unique siblings of the matched set of elements.

Example : Demonstrating the eq(index) method

<html>
<head>
<title> eq method </title>
     <script type = "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">
     </script>
     <script type="text/javascript" language ="javascript">
          $(document).ready(function()
          {
               $("li").eq(3).addClass("selected");
          });
     </script>
     <style>
          .selected {color:red;}          //adds color to located list item
     </style>
</head>
<body>
     <div>
     <ul>
          <li> MBA </li>
          <li> MCA </li>
          <li> BE </li>
          <li> Law </li>
          <li> CA </li>
     </ul>
     </div>
</body>
</html>


Output:
  • MBA
  • MCA
  • BE
  • Law
  • CA
  • In the above example, every list has its own index so they can be located directly by using the eq(index) method.
  • By default, every child element starts with index zero, therefore $(“li”).eq(0) refers to MBA.

Example : Demonstrating for filter (selector)

<html>
<head>
<title> filter method </title>
     <script type = "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">
     </script>
     <script type = "text/javascript" language = "javascript">
     $(document).ready(function()
     {
          $("li").filter(".c2").addClass("selected");
     });
     </script>
     <style>
          .selected {color:blue;}          //adds color to the lists associated with the middle class
     </style>
</head>
<body>
     <div>
     <ul>
          <li class="c1"> MBA </li>
          <li class="c1"> MCA </li>
          <li class="c2"> BE </li>
          <li class="c3"> Law </li>
          <li class="c2"> CA </li>
     </ul>
     </div>
</body>
</html>


Output:
  • MBA
  • MCA
  • BE
  • Law
  • CA
In the above example, filter (selector) method filters out all elements from the set of matched elements that do not match the specified selector.

Refining selections

The find(selector) helps in locating all the descendant elements of a particular type of elements.

Example : Demonstrating the find(selector) method

<html>
<head>
<title> refining selections</title>
     <script type = "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">
     </script>
     <script type="text/javascript" language ="javascript">
     $(document).ready(function()
     {
          $("p").find("span").addClass("selected");
     });
     </script>
     <style>
          .selected {color:red;}          //adds color to the lists associated with the middle class
     </style>
</head>
<body>
          <p> 1st para:<span>TutorialRide</span></p>
          <p> 2nd para:<span>E-Learning</span></p>
</body>
</html>


Output:

1st para: TutorialRide
2nd para: E-Learning

In the above example, the find() method will select all the <span> elements residing inside the different <p> elements.