Solved

JQuery - Show/Hide Mulitple Divs with Select

Posted on 2010-09-08
8
643 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

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

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
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…

840 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