Solved

JQuery Toggle-How do I tell if all toggled elements are hidden?

Posted on 2011-09-07
3
270 Views
Last Modified: 2012-05-12
Hello Experts,

I have a (hopefully) fairly simple question. Below i have a simple JQuery toggle menu for a recipe display which actually functions quite nicely for what we need, and ties in with a print stylesheet. We had this page done and the client added an advertisement to the side of the page, "recipe-call". Since all recipes have images that pop up on the same side, we need to hide the "recipe-call" div when someone clicks on a recipe title to display it. We have that function done just fine. As it stands, "recipe-call" is absolutely positioned and everything is fine with it, but what we need to do is get it to show up again when all of the recipes are collapsed. How would we test to see if all of the DIV elements are collapsed? Would this be a simple IF statement? I've tried a few things and so far it's been a no-go.

<script type="text/javascript">
         $(document).ready(function() {
  			$('div#recipe-show> div').hide();  
  			$('div#recipe-show> h2').click(function() {
				$('div#recipe-call').hide();
    			$(this).next('div').slideToggle('slow')
    			.siblings('div:visible').slideUp('slow');
  			});
		});
</script>

<div id="recipe-call">
        <img src="images/picture.jpg" width="259" height="196" />
        
        <div id="callout">
        <h4>Download award-winning <br>
        recipes here!</h4>
        </div>
</div>


<div id="recipe-show">
<h2>Title one</h2>
<div class="print-div">Content one</div>
<h2>Title two</h2>
<div class="print-div">Content two</div>
etc...
</div>

Open in new window


I'm a beginner with JQuery but not a beginner with development/code. Thanks in advance for your thoughts!
0
Comment
Question by:Jen0910
  • 2
3 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36496523
var all_Toggled_Element_Is_Hidden_FLAG = ( $("h2","div#recipe-show").next("div:hidden").length == $("h2","div#recipe-show").length ); // true or false
0
 
LVL 8

Author Comment

by:Jen0910
ID: 36497094
Thanks leakim971! Unfortunately, I'm having no luck. Would it work something like this:

<script type="text/javascript">
		var all_Toggled_Element_Is_Hidden_FLAG = ( $("h2","div#recipe-show").next("div:hidden").length == $("h2","div#recipe-show").length ); // true or false
		
         $(document).ready(function() {
  			$('div#recipe-show> div').hide();  
  			$('div#recipe-show> h2').click(function() {
				$('div#recipe-call').hide();
    			$(this).next('div').slideToggle('slow')
    			.siblings('div:visible').slideUp('slow');
  			});
			
			if(all_Toggled_Element_Is_Hidden_FLAG === true ) {
    			$('div#recipe-call').show();
				}
		});
</script>

Open in new window


Or am I implementing wrong?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36497133
the flag did not update magically each time you change/toggle something on the page :)

firstly you need to use it ONCe document is ready
secondly you need to understand toogle take time to change state (hidden or visible) of the element
the value of the flag need to be updated before toggle or at the end

before just put it before
at the end, use the callback function of the slideToggle

slideToggle('slow', function() { if(all_Toggled_Element_Is_Hidden_FLAG === true ) {
                      $('div#recipe-call').show();
                        }
}
);

http://api.jquery.com/slideToggle/
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

839 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