Getting Radio buttons value with Prototype.js

In Prototype.js, there’s a handy function $F to get the value of a form element.
However it doesn’t work well with a group radio buttons. The argument of $F is the id of the element, and since id is unique, we can’t use the name of the radio group to get the value, which is essentially how the server side gets the value.


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

#wrong
var typeValue = $F('type');

Here’s a quick solution.


var typeValue = Form.getInputs('myform','radio','type').find(function(radio) { return radio.checked; }).value;


  1. Gurcharan

    Thanxs..
    It’s really working and simple.

  2. Pete

    Clean, concise example. This was helpful, thanks…
    -Pete.

  3. Anders

    Hi mate, thanks for this - a unique compressed prototype :).

  4. Julio

    Hi there,
    thanks for that tip I was getting mad with that!!
    - Julio from Perú ;)

  5. Tomas Nielsen

    This is so beautiful it brings tears to my eyes. Thanks!

  6. Julien C

    Nice one-liner solution to a common problem :)

  7. Lee

    I think the form has to have an id as well as a name in the opening form tag. I had to add id=”myform” to the form tag to get this working. Awesome tip! Thanks for posting.

  8. Learn PHP

    Thanks mate. Simple and impressive solution.

  9. Jan R

    Thats very nice.

    Do you have an Solution as nice as this for doing the other way around?
    e.g.:
    setRadio( 'type', 'two' ) // checking the radio button wich value is 'two'

  10. Jan R

    Note to myself: Think before you write. :D

    It was easy to do, taking your code as base.

    $('myform').getInputs('radio','type').find( function(radio){return radio.value=='two'}).checked = true;
    or
    Form.getInputs('myform','radio','type').find( function(radio){return radio.value=='two'}).checked = true;

    I like the first Version better because it looks more OO.

  11. Keith Halstead

    Here’s an alternative, note the form tag must have an ID

    var typeValue = $(’myform’).serialize().toQueryParams()[’type’]

  12. zhiqing

    Hi everyone:
    var typeValue = Form.getInputs(’myform’,'radio’,'type’).find(function(radio) { return radio.checked; }).value;
    it can works in IE perfectly.However it doesn’t work in FireFox.

  13. Jesper Rønn-Jensen

    you could benefit from using the CSS selectors in stead like so:

    $$(’input[name=type]:checked’).first().value;

    Short and simple :)

  14. Leon

    I like it. Thanks!

  15. Munteanu Alex

    var form = $(’MyForm’);
    var typeValue = form.getInputs(’radio’, ‘RadioName’).find(function(radio) { return radio.checked; }).value;

  1. 1 James Starmer » Blog Archive » How to find which radio button is selected

    […] Rex Chung has a great one line script to find a checked radio button out of a set with the same name attribute using prototype.js. […]

  2. 2 David Bolton Strikes Again » Blog Archive » Ajax.Autocompleter with dynamic parameters

    […] I’ve often found the scriptaculous docs to be rather lacking (the jQuery docs tend to be much better for instance), and today I ran into another issue.ÂÂ While the docs for Ajax.Autocompleter do mention the callback method, it wasn’t really clear till I came across a great post by Nicholas Schlueter on dynamic parameters for Ajax.Autocompleter.ÂÂ Combined with a handy bit of javascript from my buddy Rex Chung for getting radio group values and I had a solution to my problem (of how to return parameters in my AJAX request with dynamic data from user input into a radio group) […]

  3. 3 viz : Prototype Radio Button Serialization Bug

    […] of hash) of serializing radio buttons. Google of course came to the rescue with this very excellent fix from Rex […]

  4. 4 Obtener el valor de la selección de un grupo de radiobuttons con Prototype - 6th Edition

    […] Getting Radio buttons value with Prototype.js. http://rexchung.com/2007/02/22/getting-radio-buttons-value-with-prototypejs/ […]



Leave a Comment




HK Web & Tech Meetup Index

HK Ruby on Rails User Group

My Tumblr blog

Categories



    Friends blog


    Read Write Sleep
    雨樂文康匯
    852 Signal
    Enjoy Rails
    Gang Lu
    Web Wednesday

    RoRCraft

    I am the founder of RoRCraft Ltd., a web consultancy firm that develops usable and speedy web applications for our clients.


    Based in Sydney, I travel to Hong Kong and HangZhou China to meet with my team regularly. I love exploring new technologies and business ideas that helps making our world a better place.



    An online video conversion tool. It allows everyone to freely convert their videos for ipods, mobiles and flash.


    Rails Job


    Job board for Ruby and Rails developers.


    View Rex Chung's profile on LinkedIn

    www.flickr.com

    LATEST RUBY JOBS

    Embed this on your site?