If I have a selection
item 1
item 2
item 3
Either of these:
In your case, eq
or :nth-child
are suitable. Eg:
var item1 = $('#container > :nth-child(1)'); // parent > child (=first child)
var item2 = $('#container').children().eq(1); // Zero-based indexes
$('#container').children().eq(1);
You can use :eq selector like below,
$('#container div:eq(2)') //will return you div with item 2
Use eq()
method.
$("#container > div").eq(1)
You can even use :eq(1)
pseudo selector.
$("#container > div:eq(1)")
.eq(index)
reduces the set of matched elements to the one at the specified index.
:eq(index)
selects the element at index n within the matched set.
Try:
$("#container div").eq(1) // zero-based, so will select 'item 2'
$('#container').children().eq(2);