AJDEV AJDEV - 5 months ago 56
jQuery Question

jQuery : Find nth-child of a element saved as a variable

var $slideObj = $('#slideshow .slideItem');
var num = 10;


I got that code and I want to find the nth-child of the "$slideObj" element. The code below works but I want to use the variable instead of finding the element again.

var $currentSlideObj = $('#slideshow .slideItem:nth-child(' + num + ')');


What I want is the following, $slideObj.nth-child(num);
I have tried the following below.

$slideObj.find(':nth-child('+num+')');
$slideObj.eq(num);

Answer

You actually already have all the "children" in your $slideObj.

You need to use .filter rather than .find (filter the current result set, not look deeper in the tree).

$slideObj.filter(':nth-child(' + num + ')');
Comments