Solved

JQuery - Show/Hide Mulitple Divs with Select

Posted on 2010-09-08
8
644 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

756 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