Solved

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

Posted on 2011-09-07
3
266 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now