Solved

JQuery - Show/Hide Mulitple Divs with Select

Posted on 2010-09-08
8
645 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
[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
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

738 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