I want to count the sibling
by classes,
html,
<div class="item-sibling">1</div>
<div class="item-holder"><div class="item-sibling">2</div></div>
<div class="item-holder"><div class="item-sibling">3</div></div>
<div class="item-holder"><div class="item-sibling">4</div></div>
<div class="item-holder"><div class="item-sibling">5</div></div>
jquery,
var len = $('.item-sibling').siblings().css({background:'red'}).length;
alert(len); // return 4
it does not include <div class="item-sibling">1</div>
how can I include it?
jsfiddle link
and if I change the html to,
<div class="item-sibling">0</div>
<div class="item-sibling">1</div>
<div class="item-holder"><div class="item-sibling">2</div></div>
<div class="item-holder"><div class="item-sibling">3</div></div>
<div class="item-holder"><div class="item-sibling">4</div></div>
<div class="item-holder"><div class="item-sibling">5</div></div>
I will get 6
this time. Strange!
EDIT,
<div class="group-a">
<div class="item-sibling">1</div>
<div class="item-holder"><div class="item-sibling">2</div></div>
<div class="item-holder"><div class="item-sibling">3</div></div>
<div class="item-holder"><div class="item-sibling">4</div></div>
<div class="item-holder"><div class="item-sibling">5</div></div>
</div>
<div class="group-b">
<div class="item-sibling">1</div>
<div class="item-holder"><div class="item-sibling">2</div></div>
<div class="item-holder"><div class="item-sibling">3</div></div>
</div>
There are series of groups with the same class, and I want to count a targeted group's sibling dynamically for instance the first group.
.item-sibling
elements but the first one have siblings... you have 4.item-holder
elements which are the siblings of the first.item-sibling
element. All other.item-sibling
elements don't have siblings. I think you didn't get the terminology right... what is your actually issue? What are you trying to achieve with that code? edit: In the other case you get 6 elements because you get the union of siblings of the first two.item-sibling
elements. – Lawrenceitem-sibling
use$('.item-sibling').lenght
– Snob.item-sibling
elements but the first one have siblings (couldn't edit anymore). – Lawrence.item-sibling
elements in that group? – Lawrence".item-sibling" elements in that group
. – Servais