Solved

Optimize my jQuery div toggle

Posted on 2015-02-08
3
114 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
[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 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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

739 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