← Back to jQuery Course | Chapter 7: DOM Traversing | Lesson 1 of 10

Introduction to DOM Traversing

DOM traversing means moving through related elements. jQuery makes this simple.

What Is Traversing?

Once you've selected one element, traversal methods let you step to its parents, children, or siblings instead of writing a brand-new selector. This is often simpler and more reliable than trying to describe a related element's position from scratch with CSS selector syntax.

Example: What Is Traversing?

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box"><p>Child paragraph</p></div>
    <script>
      console.log($("#box").children().length);
    </script>
  </body>
</html>

Using find()

Use find() when you want matching elements deeper inside a selected element, searching through all of its descendants regardless of nesting depth. It works like running a fresh selector, but scoped to only search within the elements you've already selected.

Example: Using find()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box"><ul><li>Item</li></ul></div>
    <script>
      console.log($("#box").find("li").length);
    </script>
  </body>
</html>

Using first() and last()

first() and last() narrow a selection down to just its first or last matched element, which is useful after a broader selection like find() or filter() returns several results. Both return a new jQuery object containing at most one element.

Example: Using first() and last()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <ul id="list"><li>One</li><li>Two</li><li>Three</li></ul>
    <script>
      console.log($("#list li").first().text(), $("#list li").last().text());
    </script>
  </body>
</html>

Chaining Traversal

Traversal methods can be chained together -- for example, finding a container, then its children, then filtering those -- building up a precise selection step by step. This keeps code short by avoiding separate selector strings for every intermediate step.

Example: Chaining Traversal

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box"><ul><li class="active">One</li><li>Two</li></ul></div>
    <script>
      console.log($("#box").find("li").filter(".active").text());
    </script>
  </body>
</html>

Practical Traversing

DOM traversal is especially useful when elements are related structurally but don't have unique IDs or classes to select directly, such as finding 'the button next to whichever input triggered an event'. It lets your code follow the actual DOM relationships instead of hardcoding selectors.

Example: Practical Traversing

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div class="row"><input id="field"><button>Next</button></div>
    <script>
      $("#field").closest(".row").find("button").on("click", function() {
      console.log("Next clicked");
      });
    </script>
  </body>
</html>

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.