Show Multiple Fields Based On Dropdown
I'm trying to show / hide fields based on the drop down menu specifying 'other', however only the first element shows, how do I make all fields with li id = 'osother' appear when s
Solution 1:
You must use ID only once in a document, so you have to change #osother
to .osother
. And also fields should be hidden when the value of dropdown is changed from osother
.
JSFiddle is here.
HTML
<ul><li><p>Operating System:
<SelectClass="selectmenu"id="whatever"name="OS"onchange="markDirty();"required><optionvalue="">-- Select an Option --</option><optionvalue="Win">Windows</option><optionvalue="ios">iOS</option><optionvalue="otheros">Other</option></select></p></li><liclass="osother">Please Specify:
<labelid="oslabel"><inputname="Other OS"type="text"placeholder="Other OS"size="50" /></label></li><liclass="osother">Version:
<labelid="version"><inputname="OSV"type="text"placeholder="OS Version"size="50" /></label></li></ul>
JS
$(document).ready(function () {
$("select").change(function () {
$("select option:selected").each(function () {
if ($(this).attr("value") == "otheros") {
$(".osother").show();
} else {
$(".osother").hide();
}
});
}).change();
});
CSS
.osother {
display:none;
}
Solution 2:
You have a few errors here:
You need to encapsulate your
li
withinul
id
needs to be unique- as it is assumed to be, the selector will only return on (the first) element, you should use e.g. a class instead, allowing you to select multiple elements within the same groupingIn your HTML you have capitalised
Class
andSelect
, these should be lowercase
Demo Fiddle
HTML
<ul><li><p>Operating System:
<selectclass="selectmenu"id="whatever"name="OS"onchange="markDirty();"required='required'></select></p></li><liclass="osother">Please Specify:
<labelid="oslabel"><inputname="Other OS"type="text"placeholder="Other OS"size="50" /></label></li><liclass="osother">Version:
<labelid="version"><inputname="OSV"type="text"placeholder="OS Version"size="50" /></label></li></ul>
JS
$(document).ready(function () {
$("select").change(function () {
$("select option:selected").each(function () {
if ($(this).attr("value") == "otheros") {
$(".osother").show();
}
});
}).change();
});
Post a Comment for "Show Multiple Fields Based On Dropdown"