[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Optimize my jQuery div toggle

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
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…

656 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