Jquery Ancestors
jQuery Traversing - Ancestors
An ancestor is a parent, grandparent, great-grandparent, together with and thence on.
With jQuery you lot tin traverse upwards the DOM tree to honour ancestors of an element.
Traversing Up the DOM Tree
Three useful jQuery methods for traversing upwards the DOM tree are:- parent()
- parents()
- parentsUntil()
jQuery parent() Method
The parent() method returns the straight nurture chemical ingredient of the selected element.This method solely traverse a unmarried score upwards the DOM tree.
The next instance returns the straight nurture chemical ingredient of each <span> elements:
Example
$(document).ready(function(){
$("span").parent();
});
jQuery parents() Method
The parents() method returns all ancestor elements of the selected element, all the agency upwards to the document's beginning chemical ingredient (<html>).The next instance returns all ancestors of all <span> elements:
Example
$(document).ready(function(){
$("span").parents();
});
The next instance returns all ancestors of all <span> elements that are <ul> elements:
Example
$(document).ready(function(){
$("span").parents("ul");
});
jQuery parentsUntil() Method
The parentsUntil() method returns all ancestor elements betwixt 2 given arguments.The next instance returns all ancestor elements betwixt a <span> together with a <div> element:
Example
$(document).ready(function(){
$("span").parentsUntil("div");
});
Comments
Post a Comment