jQuery Toggel Close all

I have this jQuery function that toggles open divs when a certain class is clicked:
<script>
jQuery(document).ready(function() {
	jQuery('.laser-nav-display').click(function() {
		jQuery('.nav-lasers').slideToggle("slow", function() {
			
		});
	});
	jQuery('.app-nav-display').click(function() {
		jQuery('.nav-applications').slideToggle("slow", function() {
			
		});
	});
	jQuery('.support-nav-display').click(function() {
		jQuery('.nav-support').slideToggle("slow", function() {
			
		});
	});
	jQuery('.company-nav-display').click(function() {
		jQuery('.nav-company').slideToggle("slow", function() {
			});
		});
	jQuery('.accept').click(function() {
		jQuery('.agreement').show("slow", function() {
			
		});
	});
});
</script>

Open in new window

However, when I click on one, I want the others to close instead oe.staing open. So there is only one open at a time.  How do I go about this?
LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
Just add slideUp() for other divs. I do not know what your divs have in common (like a name or class or something) so I do not know how to access to them as a collection. Therefore my code looks messy:
<script>
jQuery(document).ready(function() {
	jQuery('.laser-nav-display').click(function() {
		jQuery('.nav-applications').slideUp("slow");
		jQuery('.nav-support').slideUp("slow");
		jQuery('.nav-company').slideUp("slow");
		jQuery('.agreement').slideUp("slow");
		jQuery('.nav-lasers').slideToggle("slow");
	});
	jQuery('.app-nav-display').click(function() {
		jQuery('.nav-lasers').slideUp("slow");
		jQuery('.nav-support').slideUp("slow");
		jQuery('.nav-company').slideUp("slow");
		jQuery('.agreement').slideUp("slow");
		jQuery('.nav-applications').slideToggle("slow");
	});
	jQuery('.support-nav-display').click(function() {
		jQuery('.nav-lasers').slideUp("slow");
		jQuery('.nav-applications').slideUp("slow");
		jQuery('.nav-company').slideUp("slow");
		jQuery('.agreement').slideUp("slow");
		jQuery('.nav-support').slideToggle("slow");
	});
	jQuery('.company-nav-display').click(function() {
		jQuery('.nav-lasers').slideUp("slow");
		jQuery('.nav-applications').slideUp("slow");
		jQuery('.nav-support').slideUp("slow");
		jQuery('.agreement').slideUp("slow");
		jQuery('.nav-company').slideToggle("slow");
	});
	jQuery('.accept').click(function() {
		jQuery('.nav-lasers').slideUp("slow");
		jQuery('.nav-applications').slideUp("slow");
		jQuery('.nav-support').slideUp("slow");
		jQuery('.nav-company').slideUp("slow");
		jQuery('.agreement').slideToggle("slow");
	});
});
</script>

Open in new window

BTW, you do not need to use the function(){} for your slideToggle() if the function is empty. If you have a common attribute for your divs, eg all of them have a "toggling" class then you can just write this:
		jQuery('.toggling').slideUp("slow");

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.