Hiya,
I have a simple set of images nested in a div tag, I'm just wondering how I can chose an item with a specific index programatically?
var n = new Array();
n.push(a);
n.push(b);
n.push(c);
n[1]; // b
How do I do this in JQuery?
Thanks
Hiya,
I have a simple set of images nested in a div tag, I'm just wondering how I can chose an item with a specific index programatically?
var n = new Array();
n.push(a);
n.push(b);
n.push(c);
n[1]; // b
How do I do this in JQuery?
Thanks
Use the :eq(index)
selector or the .eq(index)
function, like this:
$("#myDiv img:eq(0)") //get the first one
//equal to: $("#myDiv img").eq(0)
$("#myDiv img:eq(1)") //get the second one
//equal to: $("#myDiv img").eq(1)
Alternatively, there's .get(index)
if you want the actual DOM element and not the jQuery (wrapped) element, like this:
$("#myDiv img").get(0) //get the first one
All of the above options have a zero-based index.