Sajjan Gurung Sajjan Gurung - 1 year ago 90
Javascript Question

Difference between jQuery parent(), parents() and closest() functions

I have been using jQuery for a while. I wanted to use the

selector. I also came up with the
selector. Could not find any difference between them. Is there any? If yes, what?

What is the difference between parent(), parents() and closest()?

Answer Source

closest() selects the first element that matches the selector, up from the DOM tree.

parent() selects one element up the DOM tree.

parents() method is similar to parent() but selects all the matching elements up the DOM tree.