Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following divs
<div class="Main">
    <div class="pipe">
        <div class="mine"></div>
        <div class="mine"></div>
        <div class="mine"></div>
    </div>
    <div class="pipe">
        <div class="mine"></div>
        <div class="mine"></div>
    </div>
</div>


but I want to only count divs with class = "mine" in upper div not both the divs
Thanks in Advance!!!

What I have tried:

var count = $(.pipe.mine).length;
It counts all divs with class = "mine" and shows count as 5.
Posted
Updated 27-Oct-20 0:55am

Add another class to the div you want to count

<div class="Main">
    <div class="pipe count_this">
        <div class="mine"></div>
        <div class="mine"></div>
        <div class="mine"></div>
    </div>
    <div class="pipe">
        <div class="mine"></div>
        <div class="mine"></div>
    </div>
</div>


Then use $(.count_this.mine).length
 
Share this answer
 
Comments
Ank_ush 27-Oct-20 3:21am    
Class name is the same for both, cannot be different.
Peter_in_2780 27-Oct-20 3:56am    
You still have the "pipe" class name. DOM elements can have multiple class names attached.
Did you take a close look at the solution?
Either of these should work

$(".pipe:first > .mine")


"first" is a special selector but you use "eq" with any index

$(".pipe:eq(0) > .mine")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900