Checkboxes and Radio Buttons
For some reason, Prototype still does not give developers convenient APIs for dealing with checkboxes and radio buttons. These extensions allow you to use Prototype's convenient $F() syntax to get and set the values of these grouped input elements. They've been tested with Prototype 1.6.0.3 and 1.6.1.
Example 1: Checkboxes
Which fruits do you like? Check them off and then click "Print Checked
Fruits." The one line of code doing the work simply calls
$F($("form").fruits)
, something not possible without
these extensions. That method call returns an array of values for the
checked items.
Example 2: Setting checkbox values
The extended setValue()
now also works with checkboxes.
You can use any array. The checkboxes below are set directly from the
return value of calling $F()
on the checked values above.
Other Features
This little helper script also includes methods for getting and setting the cursor position in a textarea, and an extension to Ajax.Request that allows you to abort an operation cleanly. Enjoy!