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();
});
You tin equally good utilisation an optional parameter to filter the search for ancestors.
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

Popular posts from this blog

What Are The Main Components of a Computer System

Top Qualities To Look For In An IT Support Team

How To Integrate Google Adwords Api Into Codeigniter?