I thought everyone might like to have this - I didn''t like having to do a work-around to get the value of the checked radio in a set of radio buttons, so I created $FR() - $F for radio buttons =) /** * Return the value of the given radio button * @param mixed frm form ID or object reference * @param mixed name radio name (value in name="...") * @return mixed */ function $FR(frm, name) { return $F($(frm).getInputs(''radio'', name).find(function(obj) { return obj.checked; })); } Usage: $FR(''formID'', ''radio_name'') Example: <form id="frmRadio"> <input type="radio" name="post" value="yes" checked="checked" /> Yes <input type="radio" name="post" value="no" /> No <input type="button" value="Show Value" onclick="showValue()" /> </form> <script type="text/javascript"> function showValue() { alert($FR(''frmRadio'', ''post'')); } </script> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
pawt wrote:> I thought everyone might like to have this - I didn''t like having to > do a work-around to get the value of the checked radio in a set of > radio buttons, so I created $FR() - $F for radio buttons =)It is a failing of Prototype.js that $F() does not return the value of a radio button set. [...]> function $FR(frm, name) > { > return $F($(frm).getInputs(''radio'', name).find(function(obj) { return > obj.checked; })); > }Not too flash: if no buttons are checked, it throws an error. While it is invalid HTML to have a radio button set without one button checked, about 99% (complete guess, but not too far wrong I''d reckon) of pages with radio buttons don''t have one checked by default. Consider the following which uses about the same number of characters, yet runs 2 to 4 times faster in the browsers I tested (and doesn''t error when none are checked): function rbValue(el) { var i = el.length; while(i--) if (el[i].checked) return el[i].value; return el.checked && el.value; } Call with something like: <input onclick="rbValue(this.form.buttonsetName)" ... > There are many, many libraries out there that do a good job of returning the value of form elements, including radio buttons. -- Rob --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Ah, good point; I hadn''t thought about a radio button set with no default selected - I included the function in my own project, in which I''m quite diligent in adhering to things such as having a default checked. I wholeheartedly agree with your 99% estimate, however =P Thanks for the correction and the improved function - I appreciate it! On Apr 27, 9:48 am, RobG <r...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> > Not too flash: if no buttons are checked, it throws an error. While > it is invalid HTML to have a radio button set without one button > checked, about 99% (complete guess, but not too far wrong I''d reckon) > of pages with radio buttons don''t have one checked by default. > > Consider the following which uses about the same number of characters, > yet runs 2 to 4 times faster in the browsers I tested (and doesn''t > error when none are checked): > > function rbValue(el) { > var i = el.length; > while(i--) > if (el[i].checked) return el[i].value; > return el.checked && el.value; > > } > > Call with something like: > > <input onclick="rbValue(this.form.buttonsetName)" ... > > > There are many, many libraries out there that do a good job of > returning the value of form elements, including radio buttons. > > -- > Rob--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---