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
Pingback on Jul 20th, 2007 at 5:33 am
[…] 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
Pingback on Nov 20th, 2007 at 4:26 pm
[…] 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
Pingback on Dec 9th, 2008 at 5:30 am
[…] of hash) of serializing radio buttons. Google of course came to the rescue with this very excellent fix from Rex […]
-
4
Pingback on Mar 5th, 2009 at 7:20 am
[…] 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
Latest Entries
- Hong Kong Startup Summary 2009
- Online Video Platform successes in 2009
- Want to put the upcoming HK IT Events to your blog?
- Gravatar experiment
- Ankoder @ Web Direction South
- Google Wave users are socialising in Hong Kong
- Last 6 Months = 200% on Ankoder.
- Introduction to cloud computing.
- I am planting a tree
- Are you an outlier?
Archives
- December 2009
- October 2009
- September 2009
- March 2009
- January 2009
- December 2008
- November 2008
- September 2008
- August 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- November 2007
- July 2007
- June 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- February 2006
- January 2006
- December 2005
Categories
- ankoder (7)
- australia (3)
- business (19)
- china (7)
- conference (8)
- environment (2)
- facebook (1)
- firefox (1)
- git (2)
- github (2)
- hangzhou (1)
- hong kong (9)
- interface (1)
- internet (2)
- javascript (1)
- jobboard (1)
- olympics (1)
- osx (1)
- personal (19)
- politics (6)
- railscamp (1)
- rorcraft (7)
- ruby on rails (7)
- scriptaculous (1)
- startup (4)
- sydney (1)
- taiwan (2)
- travel (5)
- trend (2)
- trends (1)
- twitter (1)
- Uncategorized (11)
- unfuddle (1)
- usability (2)
Twitter Updates
Friends blog
Read Write Sleep
雨樂文康匯
852 Signal
Enjoy Rails
Gang Lu
Web Wednesday
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.

Apr 16th, 2007 at 10:35 pm
Thanxs..
It’s really working and simple.
May 5th, 2007 at 4:54 am
Clean, concise example. This was helpful, thanks…
-Pete.
May 8th, 2007 at 4:10 am
Hi mate, thanks for this - a unique compressed prototype :).
Jun 8th, 2007 at 7:23 am
Hi there,
thanks for that tip I was getting mad with that!!
- Julio from Perú
Jul 23rd, 2007 at 8:03 am
This is so beautiful it brings tears to my eyes. Thanks!
Sep 12th, 2007 at 9:08 pm
Nice one-liner solution to a common problem
Sep 25th, 2007 at 5:44 am
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.
Oct 2nd, 2007 at 6:04 am
Thanks mate. Simple and impressive solution.
Oct 12th, 2007 at 12:41 am
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'Oct 12th, 2007 at 12:58 am
Note to myself: Think before you write.
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.
Oct 31st, 2007 at 12:41 am
Here’s an alternative, note the form tag must have an ID
var typeValue = $(’myform’).serialize().toQueryParams()[’type’]
Jan 15th, 2008 at 12:20 pm
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.
Sep 9th, 2008 at 12:25 am
you could benefit from using the CSS selectors in stead like so:
$$(’input[name=type]:checked’).first().value;
Short and simple
Apr 10th, 2009 at 5:04 am
I like it. Thanks!
Oct 16th, 2009 at 4:34 am
var form = $(’MyForm’);
var typeValue = form.getInputs(’radio’, ‘RadioName’).find(function(radio) { return radio.checked; }).value;