Solved

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

Posted on 2011-09-07
3
271 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
[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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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 …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

756 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