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
Solved

counting visible / hidden elements

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

$("h3").click(function () {
        $(this).next("p").slideToggle("fast");
      
            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>
      <div>
            <h3 class="hand">Toggle 3</h3>
            <p>
            Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam.
            </p>
      </div>
      <div>
            <h3 class="hand">Toggle 4</h3>
            <p>
            Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam.
            ut neque.
            </p>
      </div>
      <div>
            <h3 class="hand">Toggle 5</h3>
            <p>
            Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam.
            </p>
      </div>
</div>

problem:
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.

Question:
What am i doing wrong and or is that a better way to do this?
0
Comment
Question by:dgrafx
2 Comments
 
LVL 4

Accepted Solution

by:
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

0
 
LVL 25

Author Closing Comment

by:dgrafx
ID: 39202860
thanks ...
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

808 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