AJDEV AJDEV - 1 year ago 146
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.


Answer Source

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 + ')');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download