views:

46

answers:

4

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();
+3  A: 
Pointy
@Pointy - `$('#slider')` should return only 1 result, so `.filter()` probably isn't right here.
patrick dw
Oh right; duhh :-) I was just blindly copying from the question. Thanks; I'll update the answer.
Pointy
Also, `.eq` indexes from 0. Worth mentioning.
Adam
thanks so much =)
kr1zmo
A: 

Maybe the nth-child selector? For example, #slider .contentdiv:nth-child(2)?

CharlesLeaf
This *might* be right, but it's looking for elements based on position in the DOM, not position in a selected list.
Pointy
thanks so much =)
kr1zmo
+1  A: 

If .contentdiv elements are located inside the #slider element then you need .find() instead of .filter().

Any of these would work for you:

jQuery('#slider').find('.contentdiv').eq(1);
jQuery('#slider .contentdiv').eq(1);
jQuery('#slider .contentdiv:eq(1)');

replacing 1 with whatever number (or variable) you want, and ending with .html().

patrick dw
thanks so much =)
kr1zmo
+1  A: 

Hm..

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

edit...

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

Joe
@Joe - If `x` is a variable, then it needs to be concatenated into the string. Right now it will be passing an invalid character. See @Pointy's answer.
patrick dw
Ah, oops. That gets me a lot. haha. Thanks Patrick. :p
Joe
thanks so much =)
kr1zmo