Radio, Checkbox group and Select value
var radioValue = getRadio( document.forms['formName'], 'radioName' ); var checkedValues = getChecked( document.forms['formName'], 'checkName' ); var selectedValue = getSelected( document.forms['formName'], 'selectName' ); setRadio( document.forms['formName'], 'radioName', 'value' ); setChecked( document.forms['formName'], 'checkName', ['value 1', 'value 2'] ); setSelected( document.forms['formName'], 'selectName', 'value 1' );
These functions return and set the 'value' of groups of radio
buttons and checkboxes. With most <input ..>
tags, there is only one object and it has only one value. However the 'value' of a group of radio buttons or checkboxes is dependent on the
checked
attribute.
The code can be obtained from the head of this document. View source in your browser.
string = getRadio( formObject, radioName )
Returns a string representing the 'value' of the one radio
button that is checked. If none are checked the value is an empty
string. If the group could not be found the result is
undefined
.
array = getChecked( formObject, checkName )
Returns an array of the 'value's of the checked checkboxes.
If none are checked the value is an empty array. If the group
could not be found the result is undefined
.
string = getSelected( formObject, selectName )
array = getSelected( formObject, multipleSelectName )
Returns either a string or an array of the 'value's of the
selected options (depending on the multiple
attribute)
In a 'multiple' select, if none are checked the value is an empty
array is returned. If the select could not be found the result is
undefined
.
node = setRadio( formObject, checkName, value )
Searches the radio group for the given value and checks it. Returns that node on success or null otherwise.
node = setChecked( formObject, checkName, valueArray_or_String, uncheckBoolean )
Searches the checkbox group for the given value(s) and checks them whilst optionally unchecking others. Returns a list of nodes that are now checked.
node = setSelected( formObject, checkName, valueArray_or_String, optionalUncheckBoolean )
Searches the select for the given value(s) and selects them whilst optionally unselecting others. Returns a list of option nodes that are now selected.
Support for this function is provided via ##javascript on freenode IRC.
Bugs, errors and omissions excepted and accepted.
Copyright © Rick Measham, 2007. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the GPL or under the same terms as Perl at your discresion.
Rick Measham (aka Woosta)