jqueryexample.com jQuery closest() - jqueryexample.com

jQuery closest() by jqueryexample

This method works by first looking at the current element to see if it matches the specified expression, if so it just returns the element itself. If it doesn’t match then it will continue to traverse up the document, parent by parent, until an element is found that matches the specified expression. If no matching element is found then none will be returned.
Passing in a context will typically make your closest() calls much faster.
Example:
$(‘input[type=”button”]’).click(function() {
$(this).closest(‘li’).fadeOut();
});

Did you like this? Share it:

Discussion

No comments yet, be the first.

Leave a Comment