Bartek Gładys Bartek Gładys - 2 months ago 6
Javascript Question

Get specific index of element

I have following code:

<div class="something">
<p class="same"> blah </p> <!-- should return index 0 -->
</div>

<div class="something-else">
<p class="same"> blah </p> <!-- should return index 1 -->
</div>

<div class="other" >
<p class="same"> blah </p> <!-- should return index 2 -->
</div>


And my question is simply. How to get index for each paragphs, when the parents are different? I tried something like this:

$('.same').each(function() { console.log( $(this).index() });


but, obviously it returned same value for each element.

Answer
$('.same').each(function(index) { console.log( index });