Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

counting visible divs with class name jquery

Tags:

jquery

I'm having trouble using the :visible with class name.

Example:

<div class="groupedDiv" id="group1">
    <div class="level1">
        Apples
        <div class="level2"> || L2-apple </div>
    </div>

    <div class="level1" style="display:none;">
        Oranges 
        <div class="level2"> || [L2-orange] </div>
    </div>
</div>

<br/>

<div class="groupedDiv" id="group1">
    <div class="level1">
        Roses
        <div class="level2"> || L2-rose </div>
        <div class="level3"> l3 rose </div>
    </div>
</div>

If I want to count the # of visible divs of class level1

$(".level1 :visible").length; gives me output 3, doesn't this supposed to output 2 ?

$(".level1 :visible").parent().length; gives me output 2 which is correct.

I have created this example at http://jsfiddle.net/metalhawk/P87jS/

Any suggestions are helpful. Thanks

like image 727
ravi.panch Avatar asked Jan 11 '11 22:01

ravi.panch


2 Answers

$(".level1 :visible") is a descendant selector: you are selecting all visible elements that are descendants of .level1. There are three div elements that are visible beneath .level1 elements.

Use this instead:

$(".level1:visible").length;

See your fiddle per my update.

like image 68
lonesomeday Avatar answered Nov 09 '22 13:11

lonesomeday


Pretty sure you need to do $(".level1:visible").length;

the Space is breaking the code

like image 40
dpmguise Avatar answered Nov 09 '22 14:11

dpmguise