Traversing the DOM (Document Object Model) is a fundamental skill for any web developer, allowing you to navigate and manipulate the HTML elements of a webpage. jQuery makes DOM traversal straightforward with a rich set of methods that can be used to find and interact with elements based on their relationships.
DOM traversal involves moving through the DOM tree to access elements based on their relationships to other elements. jQuery provides methods to easily traverse up, down, and sideways in the DOM, allowing you to select elements based on their positions relative to other elements.
Understanding and using DOM traversal is crucial for tasks such as:
The parent()
method returns the direct parent of each element in the set of matched elements.
parent() Example
This is a child paragraph.
parent()
method selects the parent of the #child
element and applies a red border to it.#parent
div will have a red border.The children()
method returns all direct children of each element in the set of matched elements.
children() Example
Child paragraph 1
Child paragraph 2
children()
method selects all direct children of the #parent
div and changes their text color to blue.The siblings()
method returns all sibling elements of each element in the set of matched elements.
siblings() Example
First paragraph
Sibling paragraph 1
Sibling paragraph 2
siblings()
method selects all sibling elements of the #first
paragraph and changes their background color to yellow.The next()
method returns the immediately following sibling of each element in the set of matched elements.
next() Example
First paragraph
Sibling paragraph 1
Sibling paragraph 2
next()
method selects the next sibling of the #first
paragraph and makes its text bold.#first
paragraph will have bold text.The prev()
method returns the immediately preceding sibling of each element in the set of matched elements.
prev() Example
Sibling paragraph 1
Middle paragraph
Sibling paragraph 2
prev()
method selects the previous sibling of the #middle
paragraph and makes its text italic.#middle
paragraph will have italic text.The filter()
method reduces the set of matched elements to those that match the selector or pass the function’s test.
filter() Example
Paragraph 1
Paragraph 2
Paragraph 3
filter()
method selects only the paragraphs with the class special
within the set of elements with the class child
and changes their text color to red.The not()
method removes elements from the set of matched elements.
not() Example
Paragraph 1
Paragraph 2
Paragraph 3
not()
method selects all paragraphs with the class child
except those with the class special
and changes their text color to blue.The has()
method reduces the set of matched elements to those that have a descendant that matches the selector or contain a specific element.
has() Example
This is a paragraph.
This is a nested paragraph.
This is another paragraph.
has()
method selects the div
elements with the class container
that contain a descendant with the class nested
and applies a green border to them.div
with the class container
will have a green border.The find()
method gets the descendants of each element in the current set of matched elements, filtered by a selector.
find() Example
This is a paragraph.
This is a nested paragraph.
find()
method selects all elements with the class nested
that are descendants of elements with the class container
and changes their background color to yellow.The closest()
method gets the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.
closest() Example
This is a target paragraph.
closest()
method selects the closest ancestor with the class container
of the element with the class target
and applies a blue border to it.div
with the class container
will have a blue border.The parentsUntil()
method gets all ancestors up to but not including the element matched by the selector.
parentsUntil() Example
This is a target paragraph.
parentsUntil()
method selects all ancestors of the target
paragraph up to but not including the outer
div and changes their background color to light gray.middle
and inner
divs will have a light gray background.The nextUntil()
method gets all following siblings up to but not including the element matched by the selector.
nextUntil() Example
Start paragraph
Middle paragraph 1
Middle paragraph 2
End paragraph
nextUntil()
method selects all following siblings of the start
paragraph up to but not including the end
paragraph and changes their text color to green.The prevUntil()
method gets all preceding siblings up to but not including the element matched by the selector.
prevUntil() Example
Start paragraph
Middle paragraph 1
Middle paragraph 2
End paragraph
prevUntil()
method selects all preceding siblings of the end
paragraph up to but not including the start
paragraph and makes their text bold.Highlighting the direct parent of an element when it is clicked.
Highlight Parent Example
Click me to highlight my parent.
child
is clicked, the parent()
method is used to select its parent and change its background color to light blue.div
with a light blue background.Changing the text of all sibling elements of a clicked element.
Select and Modify Siblings Example
Click me to change my siblings' text.
Sibling paragraph 1
Sibling paragraph 2
Explanation:
target
is clicked, the siblings()
method selects all its siblings and changes their text.Output:
target
paragraph will change the text of the sibling paragraphs to “Text changed!”.Finding and styling a specific element within a nested structure.
Complex Hierarchy Example
This is a target paragraph.
find()
method is used to navigate through the nested structure and select the target
paragraph, applying a purple border to it.target
paragraph will have a purple border.Traversing the DOM with jQuery is a powerful way to interact with and manipulate elements on a webpage. From basic traversal methods like parent(), children(), siblings(), next(), and prev() to advanced techniques such as find(), closest(), parentsUntil(), nextUntil(), and prevUntil(), jQuery provides a rich set of tools to navigate the DOM efficiently. Happy coding !❤️