CoderZone.org

Category: >> JavaScript >> Get and set radio buttons with Javascript Bookmark and Share

<< lastnext >>

Snippet Name: Get and set radio buttons with Javascript

Description: Easy functions to get and set radio buttons with Javascript. Super handy!

Comment: (none)

Author: CoderZone
Language: JAVASCRIPT
Highlight Mode: JAVASCRIPT
Last Modified: February 27th, 2009

// The getRadio(radio) function returns the 
// value of the active radio button. 
 
function getRadio(radio){
 
         // Parameters:
            // "radio" is a radio button object, <formname>.<radiogroup>
 
         // Behavior:
            // The value of the active radio button is returned to the caller
 
   for(i=0; i<radio.length; i++){
      if(radio[i].checked){
         return(radio[i].value);
         break;
      }
   }
}
 
 
// The setRadio(radio, value) function sets the 
// active radio button that matches the value.
 
function setRadio(radio, value){
 
         // Parameters:
            // "radio" is a radio button object, <formname>.<radiogroup>
            // "value" is the radio button to activate
 
         // Behavior:
            // Sets the active radio button
 
   for(i=0; i<radio.length; i++){
      if(radio[i].value == value){
         radio[i].checked = true;
      } else {
         radio[i].checked = false;
      }
   }
 
}
 
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 8