Home > UI/UX Development > jQuery > jQuery: Difference between parent(), parents() and parentsUntil() methods.

jQuery: Difference between parent(), parents() and parentsUntil() methods.

This post was most recently updated on September 26th, 2017

Definition:

  • parent() – Use this method to get direct parent element of the current selected element.
  • parents() – Use this method to get all parent elements of the current selected element.
  • parentsUntil() – Us this method to get all ancestor elements between the selector and the parameter you have selected in the parentsUntil() method. In this method parameter inside the parentsUntil(‘#parent1’) is essential part, if you missed to use parameter it will work like parents() method.

See the difference between these methods with example given below:

HTML:

CODE:

Output:

parent

Explanation: In the above example see there are green colour box with paragraph with four other boxes with a specific border colours and just below to that there are four buttons.

Now check the difference between each buttons.

When you click on parent() button it will set yellow background colour to direct parent of green box which in purple colour border.

parents() method

When you click on parents() button it will set yellow background colour to all parent elements of the green colour box till the body.

When you click on parentsUntil(parameter) button it will set yellow background colour up to the black border colour box because we have given parameter “parentsUntil(‘#parent1’)” and “#parent1” is a blue border box.

parentsUntil() method

Now click on last button parentsUntil() it will behave like parents() method because we have not given any parameter in the parentsUntil() method.

Click here to check all these methods in DEMO page

 

This Article is TAGGED in , , , . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">