Is there a simple way, using JavaScript, to dynamically show/hide content in a <div>
based on the users selection from a drop down menu? For example, if a user selects option 1
then I would like <div>
1 to be displayed and all other <div>
s to be hidden.
EDIT: Example HTML
Setup
<select>
<option> Option 1</option>
<option> Option 2</option>
<option> Option 3</option>
<select>
<div id="content_1" style="display:hidden;">Content 1<div>
<div id="content_2" style="display:hidden;">Content 2<div>
<div id="content_3" style="display:hidden;">Content 3<div>
Meh too slow. Here's my example anyway :)
http://jsfiddle.net/cqDES/
I am not a coder, but you could save a few lines:
Here is demo.
There are many ways to perform your task, but the most elegant are, I believe, using css. Here are basic steps
This works pretty well if there a few divs, since more elements you want to toggle, more css rules should be written. Here is more general solution, binding action, base on following steps: 1. find all elements using some selector (usually it looks like '.menu-container .menu-item') 2. find one of found elements, which is current visible, hide it 3. make visible another element, the one you desire to be visible under new circumstances.
javascript it a rather timtoady language )
here is a jsfiddle with an example of showing/hiding div's via a select.
HTML:
jQuery:
The accepted answer has a couple of shortcomings:
Considering the above, your options could even have different values, but toggle the same class:
jQuery:
CSS:
Here's a JSFiddle to see it in action.
With zero jQuery