Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set radio button status with JavaScript

What method would be best to use to selectively set a single or multiple radio button(s) to a desired setting with JavaScript?

like image 512
vinomarky Avatar asked Feb 28 '12 04:02

vinomarky


People also ask

Can radio button be unchecked?

To set a radio button to checked/unchecked, select the element and set its checked property to true or false , e.g. myRadio. checked = true . When set to true , the radio button becomes checked and all other radio buttons with the same name attribute become unchecked.

How do you make a radio button unchecked by default?

When you add the radio button, go to the properties for the button, then the Options tab and 'Button checked by default' should be blank, not checked. As for JavaScript in Acro Pro 9, go to Advanced > Document Processing and in the lower part, you will see different options for JavaScript actions. Hope this helps!


2 Answers

Very simple

radiobtn = document.getElementById("theid"); radiobtn.checked = true; 
like image 106
Starx Avatar answered Sep 28 '22 01:09

Starx


the form

<form name="teenageMutant">   <input value="aa" type="radio" name="ninjaTurtles"/>   <input value="bb" type="radio" name="ninjaTurtles"/>   <input value="cc" type="radio" name="ninjaTurtles" checked/> </form> 

value="cc" will be checked by default, if you remove the "checked" non of the boxes will be checked when the form is first loaded.

document.teenageMutant.ninjaTurtles[0].checked=true; 

now value="aa" is checked. The other radio check boxes are unchecked.

see it in action: http://jsfiddle.net/yaArr/

You may do the same using the form id and the radio button id. Here is a form with id's.

<form id="lizardPeople" name="teenageMutant">   <input id="dinosaurs" value="aa" type="radio" name="ninjaTurtles"/>   <input id="elephant" value="bb" type="radio" name="ninjaTurtles"/>   <input id="dodoBird" value="cc" type="radio" name="ninjaTurtles" checked/> </form> 

value="cc" is checked by default.

document.forms["lizardPeople"]["dinosaurs"].checked=true; 

now value="aa" with id="dinosaurs" is checked, just like before.

See it in action: http://jsfiddle.net/jPfXS/

like image 44
gaby de wilde Avatar answered Sep 28 '22 01:09

gaby de wilde