Show a Certain Div's Contents if a Certain Option is Selected in a Select

A client needed me to show a certain hidden div on the page if a certain option was selected in a dropdown box. Here is the javascript/jquery code I used to show the div when the option was selected. I had multiple items on the page with the same dropdown box so I also needed to know from which dropdown box the selection came.

//
<script>
 function process_select(sel,id) {
    $('#'+sel.value+'_'+id).show(); 
 })
 }
</script>

<div id="who_1" style="display:none">Show the 'who' content #1</div>
<div id="who_2" style="display:none">Show the 'who' content #2</div>
<div id="what_1" style="display:none">Show the 'what' content #1</div>
<div id="what_2" style="display:none">Show the 'what' content #2</div>
<div id="now_1" style="display:none">Show the 'now' content #1</div>
<div id="now_2" style="display:none">Show the 'now' content #2</div>
<div id="then_1" style="display:none">Show the 'then' content #1</div>
<div id="then_2" style="display:none">Show the 'then' content #2</div>

<select name="test" id="test" onchange="process_select(this,1)">
    <option value="who">Who</option>
    <option value="what">What</option>
    <option value="now">Now</option>
    <option value="then">Then</option>
</select>

<select name="test2" id="test2" onchange="process_select(this,2)">
    <option value="who">Who</option>
    <option value="what">What</option>
    <option value="now">Now</option>
    <option value="then">Then</option>
</select>
//

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top