Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Get the second or x div with certain class

This should be simple, but I can't figure it out

For example, lets assume the class .contentdiv is what were searching for.

I want to obtain (or select) the second or (x amount) .contentdiv in a document then get the html of that div.

x being the div i want to select so pretend x is 1,2 or 3 or any number

jQuery('#slider').filter('.contentdiv').match(x).html();
like image 639
kr1zmo Avatar asked Feb 27 '23 13:02

kr1zmo


1 Answers

There are a couple of ways, but:

$('#slider').filter('contentdiv').eq(x).html();

also

$('#slider').filter('.contentdiv:eq(' + x + ')').html();

but that's messier (in my opinion).

edit — thanks @patrick: the initial selector is selecting a single element (of necessity, because "id" values have to be unique). Perhaps you meant $('#slider div.contentdiv') which would get all the <div> elements under` the "slider" container.

And another good comment further clarifies that the indexing of .eq() and the ":eq()" selector thingy is zero-based.

like image 189
Pointy Avatar answered Mar 07 '23 10:03

Pointy