Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

how to select the nth-child of the element with jquery?

We can use pseudo-selector the nth-child like this $('.className:nth-child(8)')

but what can I do if I want to use like this:

var cname = $('.className');
cname:nth-child(8); // this way obviously not work

Or want to use like this
var $this = $(this);
$this.nth-child(8); // I don't think so it would work

So, How can I implement this with jquery?

Upvotes: 0

Views: 116

Answers (3)

rajesh kakawat
rajesh kakawat

Reputation: 10896

try something like this

  $(':nth-child(8)','.className');

EDITED CODE

  $(':nth-child(8)',this);

Upvotes: 0

Navin Rauniyar
Navin Rauniyar

Reputation: 10525

You can use eq method instead of nth-child but it starts with 0

So try this:

var cname = $('.className');
cname.eq(7); 

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

If you want className elements which are the 8th child of its parent - then use .filter()

cname.filter(':nth-child(8)');

If you want className element at the index 8 of the given set

cname.eq(8);

Upvotes: 4

Related Questions