www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] How do you disable radio Group?

  1. #1
    Join Date
    Oct 2006
    Posts
    392

    resolved [RESOLVED] How do you disable radio Group?

    Does anyone know how to disable a Radio group if one of them is checked?

  2. #2
    Join Date
    Mar 2005
    Location
    Craiova
    Posts
    154
    ... 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" />

  3. #3
    Join Date
    Oct 2006
    Posts
    392
    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>

  4. #4
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,774
    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();
    };

  5. #5
    Join Date
    Oct 2006
    Posts
    392
    that worked perfect thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles