Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
counting visible divs with class name 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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

$(".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.

over 4 years ago · Santiago Trujillo Report

0

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

the Space is breaking the code

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!