Difference between jQuery parent() and parents() method?

The basic difference is the parent() function travels only one level in the DOM tree, where parents() function search through the whole DOM tree.

To understand this, let's look at the below-given HTML code.


When you make a call to parent() function like

$("#spnText").parent()
It will give you "P" as the output.parent() function selects the first parent in the DOM tree.

Now,if we call parents() function like

$("#spnText").parents()

It will give all parents in DOM tree which are, p->dvChild->dvParent->form->body->html.

You can pass a filter in parents() function  as well to select specific parent like if you want to select both the divs then

$("#spnText").parents('div');


Comments

Popular posts from this blog

Basic Terminology

Introduction To PHP

What is normalization in DBMS?