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:
<Select Class="selectmenu" id="whatever" name="OS" onchange="markDirty();" required>
<option value="">-- Select an Option --</option>
<option value="Win">Windows</option>
<option value="ios">iOS</option>
<option value="otheros">Other</option>
</select>
</p>
</li>
<li class="osother">Please Specify:
<label id="oslabel">
<input name="Other OS" type="text" placeholder="Other OS" size="50" />
</label>
</li>
<li class="osother">Version:
<label id="version">
<input name="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:
<select class="selectmenu" id="whatever" name="OS" onchange="markDirty();" required='required'></select>
</p>
</li>
<li class="osother">Please Specify:
<label id="oslabel">
<input name="Other OS" type="text" placeholder="Other OS" size="50" />
</label>
</li>
<li class="osother">Version:
<label id="version">
<input name="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"