-->

28 April 2021

jQuery Nth Child Selector

  Asp.Net CS By Example       28 April 2021
jQuery to select the Nth Child

 In this post, we are learning about jQuery to select the Nth Child selector. jQuery allows we to select not just the first or last child element, but also the nth child element. To select the nth child element, we use the nth-child selector.

 nth-child(n) 

 where n is the index number of the child.


 For example, to match the third child element, we use this syntax:

 nth-child(3) 

 To match the third child <p> element, we use this selector.

 p:nth-child(3) 


 Below code example puts for see above jQuery to select the Nth Child selector working.

jqscript.html
<html>
<head>
 <title>
   Selecting the third child
 </title>
 <script src="http://code.jquery.com/jquery-latest.js"></script>
 <script type="text/javascript">
  function setStyle()
  {
    $('p:nth-child(3)').css("font-style", "italic");
  }
 </script>
 
</head>
<body>
 <h1>Selecting the third child</h1>
 <div>
  <p>This is paragraph 1.</p>
  <p>This is paragraph 2.</p>
  <p>This is paragraph 3.</p>
  <p>This is paragraph 4.</p>
 </div>
 <form>
  <input type="button"
    value="Select"
    onclick="setStyle()"
  </input>
 </form>
</body>
</html>


Output:

logoblog

Thanks for reading jQuery Nth Child Selector

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.