Skip to content
jQuery

Traversing the DOM

Navigate relatives and filter the matched set.

#traversing#navigation

Code

jquery
// Moving around the tree
$("#item").parent();           // direct parent
$("#item").parents(".wrap");   // nearest ancestor matching
$("#item").children(".active");// direct children
$("#item").find("a");          // all descendants

// Siblings
$("#item").next();
$("#item").prev();
$("#item").siblings();

// Filtering
$("li").first();
$("li").last();
$("li").eq(2);                 // third item (0-indexed)
$("li").filter(".active");
$("li").not(".disabled");

// Chaining traversal
$("#start").parent().find("a").first().addClass("link");