...

View Full Version : Show and hidden radio button depending on Dropdown selection



Cornelis
01-10-2012, 07:41 AM
Dear Javascript programmers,

Do you have an example of a Javascript something like this:

A dropdownlist with values "one" and "two". The radio button is not visible (as a start).
- If you have selected "one" from the dropdown list, then a radio button will appear.
- If you have selected "two" from the dropdown list, then a radio button will NOT appear.

Thanks,

Cornelis
Gouda - The Netherlands

Philip M
01-10-2012, 09:07 AM
In IE you may not have a single radio button in a group - if there is only one choice use a checkbox instead.


<form name = "myform">
<select name = "mySelect" onchange = "showRadioButtons()">
<option value = "0">Select ......</option>
<option value = "1">One</option>
<option value = "2">Two</option>
</select>

<div id = "rads" style="display:none">
Check a radio button 1<input type = "radio" name = "rad1">2<input type = "radio" name = "rad1">
</div>
</form>

<script type = "text/javascript">

function showRadioButtons() {
document.getElementById("rads").style.display="none"; // hide the radios
var val = document.myform.mySelect.value;
if (val == 1) {
document.getElementById("rads").style.display="block"; // show the radios if One selected
var r = document.myform.rad1;
for (var i =0; i<r.length; i++) { // uncheck the radio previously selected
r[i].checked = false;
}
}

}

</script>


"I'm such a good lover because I practice a lot on my own." - Woody Allen



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum