How to get the nth child without any selector in Jquery ?

This post shows you that how to get the any child without any selector like id,class or else.
Let’s see some examples.
Example 1: Using ul & li

<ul>
<li>Cofee </li>
<li>Tea </li>
<li>Beer </li>
<li>Water </li>
</ul>

Case 1 : If you want to get the 2nd child text of ul
$(‘ul > :nth-child(2)’).text();
or
$(‘ul’).children(‘:first-child’).next().text();

Case 2 : If you want to get the 3rd child property of ul
$(‘ul > :nth-child(3)’).anyproperty();

Example 2: Using div

<div>
<div>Cofee </div>
<div>Tea </div>
<div>Beer </div>
<div>Water </div>
</div>

Case 1 : If you want to get the 2nd child text of div
$(‘div > :nth-child(2)’).text();
or
$(‘div’).children(‘:first-child’).next().text();

Case 2 : If you want to get the 3rd child property of div
$(‘div > :nth-child(3)’).anyproperty();

Please follow and like us:

Leave a Reply

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