Traversing means moving through the DOM tree to find elements related to the current element. It allows navigation upward, downward, and sideways in the DOM.
// Common jQuery traversing methods
parents()
parent()
children()
find()
siblings()
next()
prev()
filter()
not()
// jQuery traversing interactions
$(document).ready(function () {
$('#btnParents').click(function () {
let parents = $('#item1-1').parents().map(function () {
return this.id || this.tagName.toLowerCase();
}).get().join(', ');
$('#result').text('Parents of Item 1.1: ' + parents);
});
$('#btnChildren').click(function () {
let children = $('#item2').children().map(function () {
return this.id || this.tagName.toLowerCase();
}).get().join(', ');
$('#result').text('Children of Item 2: ' + children);
});
});
Clicking buttons dynamically displays related elements based on traversal method used.
parents() carefully for performancechildren() when structure is knownfind() with filter()