Use jQuery’s $.closest() vs $.parents()

Share this article

So I guess you should consider using $.closest()
instead of $.parents(). But first, as always you need to know what your dealing with – the key difference between them.
.closest() .parents()
Begins with the current element Begins with the parent element
Travels up the DOM tree until it finds a match for the supplied selector Travels up the DOM tree to the document’s root element, adding each ancestor element to a temporary collection; it then filters that collection based on a selector if one is supplied
The returned jQuery object contains zero or one element for each element in the original set The returned jQuery object contains zero or more elements for each element in the original set
This jsperf shows the speed results. 22-04-2013 12-09-37 AM

Get parent container using $.parents()

Code example.
//remove handler
$('.btn.remove').on('click', _this.cache.$container, function(e)
{
     e.preventDefault();
     console.log('remove...');
     $(this).parents('li').first().remove();
});

Get parent container using $.closest()

Code example.
//remove handler
$('.btn.remove').on('click', _this.cache.$container, function(e)
{
     e.preventDefault();
     console.log('remove...');
     $(this).closest('li').remove();
});

Frequently Asked Questions (FAQs) about jQuery’s Closest and Parents

What is the main difference between jQuery’s closest and parents methods?

The main difference between the two methods lies in their functionality. The closest() method begins its search with the element itself before moving up the DOM tree, and it stops once it finds a match. On the other hand, the parents() method only starts searching from the parent of the element and moves up the DOM tree, returning all ancestors that match the given selector.

Can the closest() method return multiple elements?

No, the closest() method can only return one element. It starts with the element itself and moves up the DOM tree, stopping once it finds the first match. If there are multiple matching elements, it will only return the first one it encounters.

How does the parents() method handle multiple matches?

Unlike the closest() method, the parents() method can return multiple elements. It starts from the parent of the element and moves up the DOM tree, returning all ancestors that match the given selector. The returned elements are in reverse order; the closest parent comes first.

Can I use the closest() method without a selector?

Yes, you can use the closest() method without a selector. In this case, the method will return the first parent element it encounters, regardless of its type.

How can I use the closest() method to find a specific parent element?

To find a specific parent element using the closest() method, you need to pass the selector of the desired element as an argument. The method will then return the first parent element that matches the selector.

What happens if the parents() method doesn’t find a match?

If the parents() method doesn’t find a match, it will return an empty jQuery object. This means that you can still call jQuery methods on it, but they won’t have any effect.

Can I use the parents() method to find a specific ancestor element?

Yes, you can use the parents() method to find a specific ancestor element. Just pass the selector of the desired element as an argument, and the method will return all matching ancestor elements.

How can I use the closest() and parents() methods to traverse the DOM tree?

You can use the closest() and parents() methods to traverse the DOM tree by passing the selector of the desired element as an argument. These methods will then return the matching elements, allowing you to manipulate them using other jQuery methods.

Are the closest() and parents() methods case-sensitive?

Yes, the selectors used with the closest() and parents() methods are case-sensitive. This means that ‘div’ and ‘DIV’ would refer to different elements.

Can I chain the closest() and parents() methods with other jQuery methods?

Yes, like all jQuery methods, closest() and parents() return jQuery objects, allowing you to chain them with other jQuery methods. For example, you could use the closest() method to find a specific parent element and then use the css() method to change its style.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week