I think this should be simple but I haven't been able to figure it out:
I have a container with a couple of children:
<div id='container'>
<div id='one'>some</div>
<div id='two'>random</div>
<div id='three'>text</div>
</div>
I want to be able to know the position of say element $('#two')... which should return 2 (as the element is the second of the container's children, $('#one').... should return 1 and so on.
Thanks a lot in advance,
Martin
I think this should be simple but I haven't been able to figure it out:
I have a container with a couple of children:
<div id='container'>
<div id='one'>some</div>
<div id='two'>random</div>
<div id='three'>text</div>
</div>
I want to be able to know the position of say element $('#two')... which should return 2 (as the element is the second of the container's children, $('#one').... should return 1 and so on.
Thanks a lot in advance,
Martin
Share Improve this question asked Jan 30, 2011 at 16:47 HoffHoff 39.9k17 gold badges83 silver badges104 bronze badges1 Answer
Reset to default 7You can use jQuery.index for this:
$('#two').index() // 1
Note that the index is zero-based.