Reputation: 25
I'm trying to set up a mechanism where I have multiple select dropdowns on a page, followed by divs by that are shown or hidden based on the active selection. These dropdowns and divs, and their values, are dynamically pulled from on a database.
Currently, I'm using the following jquery script.
jQuery(document).ready(function () {
jQuery(".drop-down-show-hide").hide();
jQuery("#dropDown").change(function () {
jQuery(".drop-down-show-hide").hide();
jQuery("#" + this.value).show();
});
});
And the following html code;
<select id="dropDown">
<option>Select...</option>
<option value="choice-1">Choice 1</option>
<option value="choice-2">Choice 2</option>
<option value="choice-3">Choice 3</option>
</select>
<div id="choice-1" class="drop-down-show-hide">content</div>
<div id="choice-2" class="drop-down-show-hide">content</div>
<div id="choice-3" class="drop-down-show-hide">content</div>
<select id="dropDown">
<option>Select...</option>
<option value="choice-4">Choice 4</option>
<option value="choice-5">Choice 5</option>
</select>
<div id="choice-4" class="drop-down-show-hide">content</div>
<div id="choice-5" class="drop-down-show-hide">content</div>
With this, only the first dropdown works as desired: selecting one of the choices will show that choice and hide the others. The other dropdown doesn't function - it won't show choices 4 or 5. It also doesn't hide choices 1-3, so this dropdown isn't working at all.
The behavior that I'd actually like to achieve is where each dropdown show/hides only the divs that are given as options. I should be able to give each dropdown a unique id based on the database, so you'd have something like;
<select id="uniquevalue1">
<select id="uniquevalue2">
My question, then, is how to tailor the jquery so that it can read and process the unique values. I can't hard code the unique id values into the script (like I did with dropDown") because they are pulled on the fly from a database. Is there a way to rewrite the jquery to apply the desired behavior to any dropdown on the page, or an alternative means?
Upvotes: 1
Views: 1881
Reputation: 29683
Change id
of select
to class instead. id
should always be unique in DOM
. But class
can be applied to multiple elements
<select class="dropDown">
...
</select>
<select class="dropDown">
....
</select>
and then your same code works!
UPDATE
To achieve your current requirement I suggest you to group your dropdowns as below:
<div class="group"> <!--group each select and subsequent divs into a container-->
<select class="dropDown">
<option>Select...</option>
<option value="choice-1">Choice 1</option>
<option value="choice-2">Choice 2</option>
<option value="choice-3">Choice 3</option>
</select>
<div id="choice-1" class="drop-down-show-hide">content1</div>
<div id="choice-2" class="drop-down-show-hide">content2</div>
<div id="choice-3" class="drop-down-show-hide">content3</div>
</div>
<div class="group"> <!--Same here for 2nd dropdown-->
<select class="dropDown">
<option>Select...</option>
<option value="choice-4">Choice 4</option>
<option value="choice-5">Choice 5</option>
</select>
<div id="choice-4" class="drop-down-show-hide">content4</div>
<div id="choice-5" class="drop-down-show-hide">content5</div>
</div>
JS you can update a single line in change
event:
jQuery(".dropDown").change(function () {
jQuery(this).closest('.group').find(".drop-down-show-hide").hide();
//find select's parent and hide its corresponding divs only
jQuery("#" + this.value).show();
});
Upvotes: 2