?
Solved

counting visible / hidden elements

Posted on 2013-05-28
2
Medium Priority
?
241 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

809 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