Solved

JQuery - Show/Hide Mulitple Divs with Select

Posted on 2010-09-08
8
646 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

726 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