Does anyone know how to disable a Radio group if one of them is checked?
Printable View
Does anyone know how to disable a Radio group if one of them is checked?
... You mean, make it so you can only pick one radio button in three?
Give them the same name attribute.
<input type="radio" name="NAME" value="1" />
<input type="radio" name="NAME" value="2" />
<input type="radio" name="NAME" value="3" />
No what I am trying to do is if one is checked onload then disable all of them in that group. This is what I have so far but it only diables the one checked.Code:<script type="text/javascript">
<!--
function disableCheckBox() {
var obj=document.forms["form1"];
for (i=0;i<obj.length;i++) {
with (obj[i]) {
if (type=='radio' && checked) {
disabled=true;
}
}
}
}
//-->
</script>
Try this function out:
Code:/**
* @class window
*
* @function disableRadioGroups
* Searches through all the FORMs on the page and disables groups of radio
* buttons if one of them is checked.
*
* @param void
* @return void
*/
function disableRadioGroups() {
var forms, el, radioGroup;
var i = end = j = elsEnd = rg = rgEnd = 0;
if (document.getElementsByTagName) {
forms = document.getElementsByTagName('form');
/* Loop through all the FORMs */
for (i, end = forms.length; i < end; i++) {
/* Loop through each form element */
for (j = 0, elsEnd = forms[i].elements.length; j < elsEnd; j++) {
el = forms[i].elements[j];
/* If this is a checked, enabled radio button in a radio group... */
if (!el.disabled
&& el.nodeName === 'INPUT'
&& el.type == 'radio'
&& el.checked
&& forms[i].elements[el.name].length) {
radioGroup = forms[i].elements[el.name];
/* Loop through radio group and disable buttons */
for (rg = 0, rgEnd = radioGroup.length; rg < rgEnd; rg++) {
radioGroup[rg].disabled = true;
}
}
}
}
}
}
window.onload = function() {
disableRadioGroups();
};
that worked perfect thanks.