Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you change an HTML radiobutton selection from Javascript?

I need to select an HTML radiobutton (deselecting any previously selected radiobutton) on my form, from within Javascript.

How is this accomplished?

like image 604
Giffyguy Avatar asked May 03 '10 21:05

Giffyguy


People also ask

How do I change the selected radio button?

Just use the android:buttonTint="@color/colorPrimary" attribute on the <RadioButton> tag.

How do I uncheck a radio in JavaScript?

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 .

How do I automatically select a radio button in HTML?

You can check a radio button by default by adding the checked HTML attribute to the <input> element. You can disable a radio button by adding the disabled HTML attribute to both the <label> and the <input> .


1 Answers

If you set the "checked" property to true on one radio button, the other button with the same name is automatically unchecked.

Thus,

document.getElementById('buttonX').checked = true;

will cause "buttonY" to be unchecked if the HTML looks like:

<input type='radio' id='buttonX' name='fred' value='X'>
<input type='radio' id='buttonY' name='fred' value='Y' checked>

edit Remember that "radio buttons" have that name because on old radios (not necessarily older than me) the station preset buttons were mechanically inter-linked such that exactly one button was pressed at all times. Fiddling with the buttons to get them all to be un-pressed was a fun but risky pastime, as most adults didn't appreciate the aesthetic appeal of a row of un-pressed radio buttons all neatly aligned.

like image 60
Pointy Avatar answered Oct 19 '22 12:10

Pointy