Solved

Optimize my jQuery div toggle

Posted on 2015-02-08
3
111 Views
Last Modified: 2015-02-16
Hi,

I'm using the following to toggle a lot of div's, but it's not optimal with all that JS.
Is it possible to automatically match... matching id/class?

<button class="div1">Toggle 'em</button>
<div class="toggle1">
	p>Hiya</p>
	<p>Such interesting text, eh?</p>
</div>

<button class="div2">Toggle 'em</button>
<div class="toggle2">
	<p>Hiya</p>
	<p>Such interesting text, eh?</p>
</div>

<button class="div3">Toggle 'em</button>
<div class="toggle3">
	<p>Hiya</p>
	<p>Such interesting text, eh?</p>
</div>

$( ".div1" ).click(function() {	
	if ( $( ".toggle1" ).is( ":hidden" ) ) {
		$( ".toggle1" ).slideDown( "normal" );
	} else {
		$( ".toggle1" ).slideUp( "normal" );
	}
});

$( ".div2" ).click(function() {	
	if ( $( ".toggle2" ).is( ":hidden" ) ) {
		$( ".toggle2" ).slideDown( "normal" );
	} else {
		$( ".toggle2" ).slideUp( "normal" );
	}
});

$( ".div3" ).click(function() {	
	if ( $( ".toggle3" ).is( ":hidden" ) ) {
		$( ".toggle3" ).slideDown( "normal" );
	} else {
		$( ".toggle3" ).slideUp( "normal" );
	}
});

Open in new window

0
Comment
Question by:kgp43
  • 2
3 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40596764
Yes. Just give your sliding divs a common class and use slideToggle();
<button class="div1">Toggle 'em</button>
<div class="toggle1 toggle">
	p>Hiya</p>
	<p>Such interesting text, eh?</p>
</div>

<button class="div2">Toggle 'em</button>
<div class="toggle2 toggle">
	<p>Hiya</p>
	<p>Such interesting text, eh?</p>
</div>

<button class="div3">Toggle 'em</button>
<div class="toggle3 toggle">
	<p>Hiya</p>
	<p>Such interesting text, eh?</p>
</div>
<script>
$('button').click(function(){
	$(this).next('.toggle').slideToggle('normal');
});
</script>

Open in new window

0
 

Author Comment

by:kgp43
ID: 40596825
Does that work with 3 (or more) buttons? Each opening a different/matching div?
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 40596856
That script will work for any number of buttons arranged in the markup with the sliding div directly following the button. Other arrangements of markup can be made to work as well.

$('button').click attaches a handler to every <button> element on the page. The $(this) inside the handler will always refer to the specific <button> element clicked.

In a real world scenario the buttons would have a class as well so you can distinguish the ones that activate the sliding divs, example, <button class="slideButton">, then you could isolate them in the script:

$('button.slideButton').click(function(){
      $(this).next('.toggle').slideToggle('normal');
});
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
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…
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)
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…

813 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

8 Experts available now in Live!

Get 1:1 Help Now