JQuery - Show/Hide Mulitple Divs with Select


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>

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

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

      $("#" + this.value).show().siblings().hide();


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gurvinder Pal SinghCommented:
you need to use this

      $("div[id6='" + this.value + "']").show().siblings().hide();
Hi, use this one:

      $("div[id*='" + this.value + "']").show();


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gurvinder Pal SinghCommented:
sorry it should have been

      $("div[id^='" + this.value + "']").show().siblings().hide();
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

Gurvinder Pal SinghCommented:
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?

@gurvinder372: yes, your script will not work for this requirement.

Your script:

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.

Gurvinder Pal SinghCommented:
ok, didn't thought of that.
something like this?

     $("div[id^='" + this.value + "']").show();
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.