Showing posts with label parent. Show all posts
Showing posts with label parent. Show all posts

Thursday, September 19, 2024

JQuery Parent vs Parents



What is the difference between ‘paren’t and ‘parents’ in jQuery?


HTML Example:

<body>body (great-great-grandparent)

  <div>div (great-grandparent)

    <ul>ul (grandparent)  

      <li>li (parent)

        <span id="sid-1">span</span>

      </li>

      <li>li (parent)

        <span id="sid-2">span</span>

      </li>

    </ul>   

  </div>

</body>



parent()

parents()

Return

immediate parent element

all ancestor elements

Syntax 1 (without selectors)

$("#sid-1").parent();

$("#sid-1").parents();

Result

li

li, ul, div, body, html

Syntax 2 (one selector)

$("#sid-1").parent("li");

$("#sid-1").parents("ul");

Result

li

ul

Syntax 3 (multiple selectors)

$("#sid-1").parent("ul, div");

$("#sid-1").parents("ul, div");

Result

-

ul, div

Blog Posts

Enhancing Performance of Java-Web Applications

Applications built with a Java back-end, a relational database (such as Oracle or MySQL), and a JavaScript-based front-end form a common and...