?
Solved

counting visible / hidden elements

Posted on 2013-05-28
2
Medium Priority
?
239 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
[X]
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
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

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

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

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
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)
Suggested Courses

777 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