counting visible / hidden elements

Posted on 2013-05-28
Last Modified: 2013-05-28
for example when using the following code:

$("h3").click(function () {
            hidden = $(this).parent().parent(".toggler").find("div > p:hidden").length;
            shown = $(this).parent().parent(".toggler").find("div > p:visible").length;
                console.log(hidden + ' : ' + shown);

<div class="toggler">
<a class="toggleall hand" onclick="return false;">Expand</a>
            <h3 class="hand">Toggle 3</h3>
            Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam.
            <h3 class="hand">Toggle 4</h3>
            Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam.
            ut neque.
            <h3 class="hand">Toggle 5</h3>
            Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam.

there are 3 toggle areas
i'm outputting the number of hidden p's & visible p's to the console
it starts off correctly but then gets behind by 1
see if you see what I mean.

What am i doing wrong and or is that a better way to do this?
Question by:dgrafx
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Ahmed Hussein earned 500 total points
ID: 39202841
Do it that way, put the code inside a callback function for the slideToggle, so you make sure it's executed after the the slide action.

$("h3").click(function () {
        $(this).next("p").slideToggle("fast", function(){
	        hidden = $(this).parent().parent(".toggler").find("div > p:hidden").length;
            shown = $(this).parent().parent(".toggler").find("div > p:visible").length;
            console.log(hidden + ' : ' + shown);

Open in new window

LVL 25

Author Closing Comment

ID: 39202860
thanks ...

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to use .length with "input" 2 32
JQuery Autocomplete Tag AJAX (Need nice script) 11 57
googleapis - and so forth 3 20
Review of the nodejs code 1 38
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question