If I have:
<div id='parent'>
<table>
<tr>
<td>
<div id='child1'></div>
</td>
</tr>
<tr>
<td>
<div id='child2'></div>
</td>
</tr>
</table>
</div>
I tried: $(‘#parent> table > tr:eq(1) > div’);
I would like to select a certain child div at its index. For example, I would like to select the second child div child2. A trivial solution is:
var div2 = $('#child2');
But I would rather like to do so with the parent div:
var div2 = $('#parent div')...get(1); // 1 is the index.
Would this be possible?
You can do it using the
:eq()selector like this:Or if it’s dynamic and you need to pass it in, use
.eq()like this:In both of these we’re using
1because it’s a 0-based index, so1is the 2nd child.