Monday, December 28, 2009

Accessing Radio Buttons Using Javascript

I thought I would post some information about radio buttons in HTML.

If you have radio buttons like this:

<input type="radio" id="button" value="1" checked>Radio1
<input type="radio" id="button" value="2" >Radio2

To keep them in the same group, you have to make sure that the id is the same.

When you try to access them in Javascript, you can not use the document.getElementById("button")

You should access them by using document.formName.button and accessing them this way will return an array. If you are trying to access the first element, you will need to write document.formName.button[0] to get the first element of the radio set.

To get the value, you will write document.formName.button[0].value and you will write
document.formName.button[0].checked to get if the radio button is checked.

0 comments: