javascript - jquery - which object of the whole set it is? -


i wonder if there easy way figure out dom object have considering in set of objects. lets have example:

i have 5 divs:

<div id="1"></div> <div id="2" class="abc"></div> <div id="3" class="abc"></div> <div id="4"></div> <div id="5" class="abc"></div> 

now, want know (first, second, fifth , on) div id="5" in set of divs class="abc". work this:

var result = $('#5').which('.abc') => 3 var result = $('#5').which('div') => 5 

is possible without looping , comparing names?

to more clear want make oposite nth-child, because nth-child need n parameter, want have n calculated.

you can use jquery's .index() function (where result zero-based):

console.log( $('#5').index('div.abc') )
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="1"></div>  <div id="2" class="abc"></div>  <div id="3" class="abc"></div>  <div id="4"></div>  <div id="5" class="abc"></div>


Comments