Skip to content
the poor coder

jQuery Traversing Methods

Poor Man
Published
Reading time
1 min read

jQuery Traversing Methods#

jQuery Traversing Methods are used to traverse through the DOM (Document Object Model) tree in order to find certain elements or perform actions on specific elements.

Some commonly used jQuery Traversing Methods are:#

  • parent(): It selects the direct parent of the selected element.
  • parents(): It selects all the ancestors of the selected element.
  • children(): It selects all the direct children of the selected element.
  • find(): It selects all the descendant elements of the selected element.
  • siblings(): It selects all the sibling elements of the selected element.

Let's say we have the following HTML structure:

Text

 <div class="container">
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>
      Item 3
      <ul>
        <li>Sub Item 1</li>
        <li>Sub Item 2</li>
      </ul>
    </li>
    <li>Item 4</li>
  </ul>
</div>

To select the direct parent of an element, we can use the parent() method:

Text

$('li').parent(); // selects the <ul> element

To select all the ancestors of an element, we can use the parents() method:

Text

$('li').parents(); // selects the <ul> and <div> elements

To select all the direct children of an element, we can use the children() method:

Text

$('ul').children(); // selects all the <li> elements

To select all the descendant elements of an element, we can use the find() method:

Text

$('ul').find('li'); // selects all the <li> elements inside the <ul> element

To select all the sibling elements of an element, we can use the siblings() method:

Text

$('li').siblings(); // selects all the sibling <li> elements

These methods can be helpful in performing various actions such as adding or removing classes, changing text or HTML content, and more.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC