jQuery closest() method

Last Updated Jul 25, 2015, 03:00:06 PM





jQuery closest() method

jQuery closest() method returns the first ancestors of the selected element that matched the selector in the DOM tree. It is able to find out the elements and their ancestors in the DOM tree and constructs a new jQuery object from the matched elements.

In jQuery, parents() and closest() are similar in action both method traverse the DOM tree.

Syntax Example

jQuery

Look at the above HTML code. The ul contains 4 list elements or ancestors. Using the jquery closest() method we can target the closest elements in each li and change its behavior


Try It Now

Other Advanced jQuery Traversing Methods you might want to learn

jQuery Traversal Reference

jQuery Basic Traversing

jQuery Traversal Filters

jQuery Traversing Methods

Sources and Credits

The content and methods in the tutorial has been referred and updated with jQuery API and The jQuery Foundation

Last Updated Jul 25, 2015, 03:00:06 PM