Posted by tech on September 10, 2008 |
|
|
(1 votes, average: 5.00 out of 5)
Loading ...
|
|
A great helper function to get the value of the selected radio button. Note that radio button groups can only have one value whereas checkboxes can have multiple values. These functions are from this link.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| function getSelectedRadio(buttonGroup) {
// returns the array number of the selected radio button or -1 if no button is selected
if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
for (var i=0; i<buttonGroup.length; i++) {
if (buttonGroup[i].checked) {
return i
}
}
} else {
if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
}
// if we get to this point, no radio button is selected
return -1;
} // Ends the "getSelectedRadio" function
function getSelectedRadioValue(buttonGroup) {
// returns the value of the selected radio button or "" if no button is selected
var i = getSelectedRadio(buttonGroup);
if (i == -1) {
return "";
} else {
if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
return buttonGroup[i].value;
} else { // The button group is just the one button, and it is checked
return buttonGroup.value;
}
}
} // Ends the "getSelectedRadioValue" function |
Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

September 18th, 2008 at 9:10 pm
Hi nice blog!!
please visit ma two blongs and kindly give me back link.
dreamz and blackpanther
thanks!!
June 19th, 2009 at 2:02 am
[...] of how many radio buttons there are in the group is shorter and simpler than the functions I posted before. The functions below came from this site. // return the value of the radio button that is checked [...]