Web Development

General discussion


Javascript: Validate Radio Buttons

By adrocknaphobia ·
Does anyone have a quick script to test if an option of a radio button has been selected?


This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

Validate radio buttons

by richardh In reply to Javascript: Validate Radi ...

Hi Adam

var checkBoxes = document.getElementsByTagName("input");
for(i=0; i<checkBoxes.length; i++){
if(checkBoxes.checked == true){
// do something

Hope this helps. Let me know if you need more info/help.


Collapse -

Validating Radio buttons

by joebevard In reply to Javascript: Validate Radi ...

If the buttons are named the same you can siple think of them as an array. if this is the button:
<input type="radio" name="btnAnswer" value="Y">
<input type="radio" name="btnAnswer" value="N">
you can reference them by this method:
if(document.all.btnAnswer[0].selected) { //"Y" is selected
else if(document.all.btnAnswer[1].selected) {
//"N" was selected
You're basically checking the selected property to see if it's value is true. Hope this helps!

Related Discussions

Related Forums