jQuery Home

Traversing

jQuery parent() Method

The parent() method returns the direct parent element of the selected element.

The following example returns the direct parent element of each <span> elements.




The parents() method returns all ancestor elements of the selected element, all the way up to the document's root element (<html>).

The following example returns all ancestors of all <span> elements.




The parentsUntil() method returns all ancestor elements between two given arguments.

The following example returns all ancestors between a <span> and a <div> element.




The children() method returns all direct children of the selected element.

The following example returns all elements that are direct children of the <div> element.




The following example returns all <p> elements with the class name "first", that are direct children of <div>.




The find() method returns descendant elements of the selected element, all the way down to the last descendant.

The following example returns all <span> elements that are descendants of <div>.




The following example returns all descendants of <div>.




The siblings() method returns all sibling elements of the selected element.

The following example returns all sibling elements of <h2>.




You can also use an optional parameter to filter the search for siblings.

The following example returns all sibling elements of <h2> that are <p> elements.




The next() method returns the next sibling element of the selected element.

The following example returns the next sibling of <h2>.




The nextAll() method returns all next sibling elements of the selected element.

The following example returns all next sibling elements of <h2>.




The nextUntil() method returns all next sibling elements between two given arguments.

The following example returns all sibling elements between a <h2> and a <h6> element.




The prev() method returns the previous sibling element of the selected element.

The following example returns the previous sibling of <h2>.




The prevAll() method returns all previous sibling elements of the selected element.

The following example returns all previous sibling elements of <h2>.




The prevUntil() method returns all previous sibling elements between two given arguments.

The following example returns all sibling elements between a <h6> and a <h2> element.




The first() method returns the first element of the specified elements.

The following example selects the first <div> element.




The last() method returns the last element of the specified elements.

The following example selects the last <div> element.




The eq() method returns an element with a specific index number of the selected elements.

The index numbers start at 0, so the first element will have the index number 0 and not 1. The following example selects the second <p> element (index number 1).




The gt() selector selects elements with an index number higher than a specified number.

Table rows with an index greater than 3 are selected. The header row denotes an index of 0.




The lt() selector selects elements with an index number less than a specified number.

Table rows with an index less than 4 are selected. The header row denotes an index of 0.




The filter() method lets you specify a criteria. Elements that do not match the criteria are removed from the selection, and those that match will be returned.

The following example returns all <p> elements with class name "intro".




The not() method returns all elements that do not match the criteria.

The following example returns all <p> elements that do not have class name "intro".