jquery count siblings does not return correct result?
Asked Answered
S

5

5

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.

Servais answered 11/5, 2012 at 15:4 Comment(6)
All .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.Lawrence
what number are you trying to find ? to count the number of elements with the class of item-sibling use $('.item-sibling').lenghtSnob
Regarding my previous comment, it's vice versa: None of the .item-sibling elements but the first one have siblings (couldn't edit anymore).Lawrence
And with "sibling" you mean .item-sibling elements in that group?Lawrence
yes ".item-sibling" elements in that group.Servais
That's different than siblings then... "siblings" is a well defined term in the DOM. See this picture.Lawrence
B
8

You can do

var len = $('.item-sibling').siblings().andSelf().css({background:'red'}).length;

Or...

var len = $('.item-sibling').parent().children().css({background:'red'}).length;

Edit: after reading your updated, I would suggest the following:

1) Add a generic "group" class to each group. E.g.,

<div class="group group-a">
    ...
</div>

2) Then take advantage of that class to find all "siblings":

var len = $('.item-sibling:first').closest('.group')
    .find('.item-sibling').css({background:'red'}).length;
Beef answered 11/5, 2012 at 15:6 Comment(2)
@lauthiamkok: Instead of trying to find a solution to your solution, please explain what your actual problem is. Maybe you don't need/want .siblings() at all! Do you just want to count the .item-sibling elements?Lawrence
After reading your updated question, I have to agree with @Felix - what you are describing isn't really siblings. In your problem, what defines a sibling? What you've really described at this point is just all elements with the same class... which would just be $('.item-sibling').length.Beef
A
2

It's because siblings are the others. Try jQuery andSelf() which includes the target element to the fetched set.

var len = $('.item-sibling')
    .siblings()
    .andSelf()
    .css({background:'red'})
    .length;

And the second HTML you have, you have 2 .item-sibling which are 0 and 1. jQuery gets 0's siblings (.item-holder, including 1) and 1's siblings (.item-holder, including 0), which makes 6 all in all.

Affine answered 11/5, 2012 at 15:7 Comment(1)
andSelf is now deprecated as of JQuery 1.8 and it's replacement is addBack().Liquate
L
2

Ok, now that we clarified what you want, if you have a reference to the group, you can simply do:

var items = $group.find('.item-sibling').length;

If you have a reference to the an .item-sibling element, do:

var items = $item.closest('.group').find('.item-sibling').length;

This assumes that each group has a common class.

If you want to get a list of number of elements in each group, you have to iterate over each group:

var num_items = $('.group').map(function() {
    return $(this).find('.item-sibling').length;
}).get();
Lawrence answered 11/5, 2012 at 15:24 Comment(3)
Thanks Felix, I also have a solution now var len = $('.item-sibling').unwrap().siblings().css({background:'red'}).length; alert(len); maybe not as good as yours but it works.Servais
I don't understand why you keep using .siblings(), but if you have fun with it ;)Lawrence
my mistake for keeping going on with sibling :-)Servais
D
1

According to jquery documentation .

"The original element is not included among the siblings, which is important to remember when we wish to find all elements at a particular level of the DOM tree."

ref : http://api.jquery.com/siblings/

Diaphragm answered 11/5, 2012 at 15:9 Comment(0)
A
0

If you want to count all divs with class="item-sibling", why not just do

$('.item-sibling').length;

This will count all 5 divs?

Arliearliene answered 11/5, 2012 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.