
22
I
Creating and Using Objects
Radio buttons
Radio buttons offer a group of options from which the user can select only one. The group is com-
posed of <input type=radio> tags with the same name attribute; the group can contain as few as
two or as many as necessary.
The input type radio, like checkbox, makes use of the checked property to see which option was
selected. The method used to figure out which of the radio buttons was chosen depends on the num-
ber of buttons used on the form:
With just two or three buttons, you may want to use a simple if-else construct to determine
■■
which radio button was selected.
If you are offering many options, you can use a loop structure to look at the
■■
checked prop-
erty of each radio button.
With only a couple of radio buttons in a group, you can examine the one radio-type item in the
array (starting with 0) to see if it was checked. In the following code, if one radio button is selected,
the variable (theChoice) is set to one value; otherwise, it is set to the other value:
if (document.forms[0].comm[0].checked == “1”)
theChoice = “left”;
else
theChoice = “right”;
When you have many radio buttons, or you don’t know how many radio buttons you might have,
use a counter loop such as the one shown in this next example from the Enhanced LineBreak object:
for (var i = 0; i < document.theForm.lbreak.length; i++) {
if (document.theForm.lbreak[i].checked) ...