faqts : Computers : Programming : Languages : JavaScript : Forms : Radio buttons

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

15 of 20 people (75%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I check a radio button in (template)group

May 23rd, 2003 10:20
Zoo Keeper,


When using a template with a group of radio buttons in may be useful to 
change the "checked" state of a button. Just focus does not work 
properly. Here is how to do it corectly.

FormName is to be changed to the name of the form
ButtonName is to be changed to the name of the button
ButtonPosition is to be changed to the numerical position the button is 
located in the form. The count starts at 0.
in the <head>
<SCRIPT LANGUAGE=JavaScript>
function changeCheck()
{
document.FormName.ButtonName[ButtonPosition].checked=true;
}
</SCRIPT>

in your body tag put the following, onLoad="changeCheck()"

An example,
document.control.goto[1].checked=true;

form name=control
input type=radio name=goto # this is 0
input type=radio name=goto # this is 1
input type=radio name=goto # this is 2

in the above form the middle radio button would display checked
you could also use this to disable a radio button by using
"disable" instead of "checked". 
You can use more than one statement in the function to change multiple 
buttons.

document.control.goto[1].checked=true;
document.control.goto[2].disable=true;
would check the second button and disable the third.