<div id="container">
<div class="aa"></div>
<div class="bb"></div>
<div class="aa"></div>
<div class="bb"></div>
<div class="bb"></div>
<div class="bb"></div>
<div class="bb"></div>
</div>
i just need to find the index of element based on particular class while looping.
Normal index will give the element index no: among the siblings.
I want to consider siblings with same class only, and give the index based on that sorted siblings.
$("#container div").each(function(){
if($(this).hasClass('bb')){
var index = $(this).index();
//will give me 1,3,4,5 etc
//I want 0,1,2 etc ie: list the index based on class
}
if($(this).hasClass('aa')){
var index = $(this).index();
//will give me 0 , 2
//I want 0,1 etc ie: list the index based on class
}
})
For
.index()you can pass a selector as a parameter (for example'.bb').As you can find in the online documentation.
So you could do something like:
jsFiddle Demo