Solved

JQuery - Show/Hide Mulitple Divs with Select

Posted on 2010-09-08
8
638 Views
Last Modified: 2012-05-10
Hello,

I got the below code from a page of examples but the one problem I have is that I have a couple divs I want to show and hide depending on the selection.

They are all around the page so just having one will not work.

Please see that I have added a||b||c to a couple and I would like it to show all of the ones with the corresponding numbers if possible.

Thank you for your help!

<!-- the select -->
<select id="thechoices">
      <option value="box1">Box 1</option>
      <option value="box2">Box 2</option>
      <option value="box3">Box 3</option>
</select>

<!-- the DIVs -->
<div id="boxes">
      <div id="box1"><p>Box 1 stuff...</p></div>
      <div id="box1b"><p>Box 1b stuff...</p></div>
      <div id="box2"><p>Box 2 stuff...</p></div>
      <div id="box2b"><p>Box 2b stuff...</p></div>
      <div id="box2c"><p>Box 2c stuff...</p></div>
      <div id="box3"><p>Box 3 stuff...</p></div>
</div>

<!-- the jQuery -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
            
<script type="text/javascript">

$("#thechoices").change(function(){
      $("#" + this.value).show().siblings().hide();
});

$("#thechoices").change();

</script>
0
Comment
Question by:movieprodw
  • 4
  • 4
8 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33633791
you need to use this
http://api.jquery.com/attribute-starts-with-selector/


$("#thechoices").change(function(){
      $("div[id6='" + this.value + "']").show().siblings().hide();
});
0
 
LVL 15

Accepted Solution

by:
StealthyDev earned 500 total points
ID: 33634222
Hi, use this one:

$("#thechoices").change(function(){
      $("#boxes").children("div").hide();
      $("div[id*='" + this.value + "']").show();
});

Regards
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33634232
sorry it should have been


$("#thechoices").change(function(){
      $("div[id^='" + this.value + "']").show().siblings().hide();
});
0
 
LVL 15

Expert Comment

by:StealthyDev
ID: 33634249
@gurvinder372: your script is just fine.

But it will not meet the need of the author.

Actually, he is hiding all the siblings of the selected node. If he use your script, all the content will vanish if it has multiple div elements.

So, @author, try the one I have prescribed.
http://www.experts-exchange.com/Programming/Languages/Scripting/AJAX/Q_26460804.html#33634222

Regards.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Expert Comment

by:gurvinder372
ID: 33634261
he only wants to show those elements whose id value starts with what is selected in drop down box, and hide others. Will my script not work as per that requirement?

0
 
LVL 15

Expert Comment

by:StealthyDev
ID: 33634281
@gurvinder372: yes, your script will not work for this requirement.

Your script:
show().siblings().hide();

If multiple divs are there, say div1 and div1b, "div1's sibling is div1b" and "div1b's sibling is div1" - in this case, all div containers will become invisible and none will be visible.

So my idea:
We will first hide all the div containers and then show only the containers matching the id.

Regards.
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33634297
ok, didn't thought of that.
something like this?

$("#thechoices").change(function(){
     $("div").hide();
     $("div[id^='" + this.value + "']").show();
});
0
 
LVL 15

Expert Comment

by:StealthyDev
ID: 33634346
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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…

758 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

21 Experts available now in Live!

Get 1:1 Help Now