Link to home
Start Free TrialLog in
Avatar of cophi
cophi

asked on

Javascript Disable Radio Buttons

I am trying to disable a group of Radio Buttons in IE 7.

This is what the html looks like.

<TD style="PADDING-RIGHT: 15px"><INPUT id=123 onclick="" type=radio value=123 name=mode><LABEL for=123>Blue</LABEL></TD>
<TD style="PADDING-RIGHT: 15px"><INPUT id=321 onclick="" type=radio value=321 name=mode><LABEL for=321>Green</LABEL></TD>

In my javascript I have tried....

for (var b = 0; b < mode.length; b++)
          mode[b].disabled = true;

... that did not work at all

I then tried

mode.disabled = true;

.... this at least disabled the first radio button.
ASKER CERTIFIED SOLUTION
Avatar of basicinstinct
basicinstinct
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HonorGod
Have the group all use the same "name" value, then iterate over the items, and set disabled=true

one moment...
btw - obviously the javascript function needs to go within <script></script> tags (in the <head> section).
<html>
<head>
<title>Enable Buttons</title>
<script language='JavaScript' type='text/javascript'>
  function Enable( name, val ) {
    var buttons = document.getElementsByName( name );
    for ( var i = 0; i < buttons.length; i++ ) {
      buttons[ i ].disabled = val;
    }
  }
</script>
</head>
<body>
<table border='1'>
  <tbody>
    <tr>
      <td><input id='r123' onclick='' type='radio' value='123' name='mode'><label for='r123'>Blue</label></td>
    </tr>
    <tr>
      <td><input id='r321' onclick='' type='radio' value='321' name='mode'><label for='r321'>Green</label></td>
    </tr>
  </tbody>
</table>
<form id='myForm' action=''>
  <input type="button" value='Enable'  onclick='Enable("mode",false)' />
  <input type="button" value='Disable' onclick='Enable("mode",true)'/>
</form>
</body>
</html>