«

»

Mar
15

Get the value of radio buttons with Prototype

In the Prototype javascript library, there’s a utility function, $F, which gets value of a form control.  It doesn’t work quite so well for Radio Buttons.

The following code does not work:

<input type="radio" name="myRadio" value="one"/>
<input type="radio" name="myRadio" value="two"/>
<input type="radio" name="myRadio" value="three"/>

<input type="button" onclick="alert($F('myRadio'))"/>

I created another function which gets you the value of the radio button:

function $RF(el) {
   return Form.getInputs($(el).form, 'radio', $(el).name).find(
     function (radioElement) {
      return radioElement.checked;
   }).value;
}

<input type="radio" name="myRadio" value="one"/>
<input type="radio" name="myRadio" value="two"/>
<input type="radio" name="myRadio" value="three"/>

<input type="button" onclick="alert($RF('myRadio'))"/>

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>