?
Solved

Optimize my jQuery div toggle

Posted on 2015-02-08
3
Medium Priority
?
117 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 2000 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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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 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…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 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