<form name="form">In HTML, the key to creating a group of buttons is giving each button the same name. In JavaScript, creating a group of like objects, known as arrays, is possible using this same concept. Simply by giving the form elements the same name, they become part of an array, their index position depending upon the element's location in the HTML form's flow. So as far as JavaScript is concerned, the above radio buttons can be referenced like this:
<input type="radio" name="group1" value="mommy" checked>
<input type="radio" name="group1" value="toys">
<input type="radio" name="group1" value="monkeys">
</form>
document.form.group1[0] // 1st button in the groupWe also have access to the individual buttons' properties:
document.form.group1[1] // 2nd button in the group
document.form.group1[2] // 3rd button in the group
document.form.group1[0].value // returns "mommy"
document.form.group1[1].value // returns "toys"
document.form.group1[2].value // returns "monkeys"
document.form.group1[0].checked // returns trueSo to find which radio in the group is checked, we'll need to loop through the radio group array, checking each button's checked property for a true value.
document.form.group1[1].checked // returns false
document.form.group1[2].checked // returns false
var found_itOnce the group has been looped through and a value has been set for the variable, you can do anything you'd like with that value including writing it to the page or sending it to a CGI script on the server.
for (var i=0; i<document.form.group1.length; i++) {if (document.form.group1[i].checked) {}
found_it = document.form.group1[i].value
}