JavaScript for a single element radio button list, revisited with Prototype

Last year I posted JavaScript for a single element radio button list, referring to a issue I had with getting the value of generated radio button list. (All the standard solutions / samples that i found assumed the there are 2 or more choices.  And I had a case were an optional field might only have one value.  So I had to detect / trap and report on the value of this boundary case.)

I recently had reason to revisit my solution  and have since corrected two things.

In my original posting I had hard coded the name attribute of the radio button array.   Very bad,very lazy!That’s been rectified by passing the value of the name attribute into the function and using the forms element collection like this:

var ne = document.forms[0].elements[name];

Much better!

I also had cause to see if I could do better using the Prototype javascript framework? How about reducing 12 lines to 1 line like this :

selected = $$(‘input:checked[name=”‘+name+'”]’).pluck(‘value’);

You gotta like that!  (I’m also using the Google AJAX Libraries API to load the latest 1.6.0.3 release.)

As I mentioned in last years post all this also works for Check Box lists as well a Radio button lists.

I have a demo of this in serbl.html, showing the js running on a single radio button list, as well as a multi value radio button list and (because I don’t expect one to take my word) single check box button list, as well as a multi value check box button list.  The javascript functions used (getSelectedOW for OldWay, and getSelectedPW for Prototype Way) are in sebl.js

Enjoy.

One Reply to “JavaScript for a single element radio button list, revisited with Prototype”

  1. Pingback: False Positives » Blog Archive » JavaScript for a single element radio button list

Leave a Reply