[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

jquery dropdown box - multiple

Posted on 2012-08-12
3
Medium Priority
?
760 Views
Last Modified: 2012-08-12
Hi, I have a page where i want jquery to toggle multiple dropdown boxes eg;

Button1 - toggles contentbox1
Button2 - toggles contentbox2
Button3 - toggles contentbox3

HTML...
<a href="#"  id="button1">One</a>
<a href="#"  id="button2">Two</a>
<a href="#"  id="button3">Three</a>

<div id="contentbox1" style="display: none;">content box one</div>
<div id="contentbox2" style="display: none;">content box two</div>
<div id="contentbox3" style="display: none;">content box three</div>

Open in new window


JQUERY...
$(document).ready(function(){
        $('#button1').toggle(function(){
            $('#contentbox1').slideDown();
        }, function(){
            $('#contentbox1').slideUp();
        });
	});

Open in new window


How would I use this same function to slide up and down different boxes with different buttons instead of duplicating the code for each button/contentbox.
...and when a box is down and I click a button to drop another box  ...how do I make the box that is already down slide up first?

Thanks
0
Comment
Question by:joethermal
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 1000 total points
ID: 38285218
try this:
var activeBox = null;
$(document).ready(function(){
  $('a[id^=button]').toggle(function(){
    if (activeBox) {
      $('#button' + activeBox).click(); /* call second toggle function to hide the active box */
    }
    activeBox = this.id.substr(6);
    $('#contentbox' + activeBox).slideDown();
  }, function(){
    $('#contentbox' + this.id.substr(6)).slideUp();
    activeBox = null;
  });
});

Open in new window

0
 
LVL 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 1000 total points
ID: 38285284
Alternative. Add class to <a> to pick up generic click. Then make the <div>'s id based on the id of the button - wht a static suffix - the rest is trivial.

<a href="#"  id="button1" class="button-box">One</a>
<a href="#"  id="button2" class="button-box">Two</a>
<a href="#"  id="button3" class="button-box">Three</a>

<div id="button1_box" style="display: none;">content box one</div>
<div id="button2_box" style="display: none;">content box two</div>
<div id="button3_box" style="display: none;">content box three</div>

Open in new window

$(document).ready(function(){
  $('a.button-box').toggle(function(){
            $('#' + $(this).attr('id') + '_box').slideDown();
        }, function(){
            $('#' + $(this).attr('id') + '_box').slideUp();
        });
	});

Open in new window

0
 
LVL 1

Author Closing Comment

by:joethermal
ID: 38286634
Thanks - just what i wanted
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

834 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