How to set radio button status with JavaScript

JavascriptHtml

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

Very simple

radiobtn = document.getElementById("theid");
radiobtn.checked = true;

Solution 2 - Javascript

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/

Solution 3 - Javascript

Vanilla Javascript:

yourRadioButton.checked = true;

jQuery:

$('input[name=foo]').prop('checked', true);

or

$("input:checkbox").val() == "true"

Solution 4 - Javascript

You can also explicitly set value of radio button:

<form name="gendersForm">
  <input type="radio" name="gender" value="M" /> Man
  <input type="radio" name="gender" value="F" /> Woman
</form>

with the following script:

document.gendersForm.gender.value="F";

and corresponding radio button will be checked automatically.

Look at the example on JSFiddle.

Solution 5 - Javascript

/**
 * setCheckedValueOfRadioButtonGroup
 * @param {html input type=radio} vRadioObj 
 * @param {the radiobutton with this value will be checked} vValue 
 */
function setCheckedValueOfRadioButtonGroup(vRadioObj, vValue) {
    var radios = document.getElementsByName(vRadioObj.name);
    for (var j = 0; j < radios.length; j++) {
        if (radios[j].value == vValue) {
            radios[j].checked = true;
            break;
        }
    }
}

Solution 6 - Javascript

Try

myRadio.checked=true

<input type="radio" id="myRadio">My radio<br>

Solution 7 - Javascript

$("#id_of_radiobutton").prop("checked", true);

Solution 8 - Javascript

I am configuring a radio button within a document fragment and tried using radiobtn.checked = true;.

That didn't work so I instead went with this solution:

radiobtn.setAttribute("checked", "checked");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvinomarkyView Question on Stackoverflow
Solution 1 - JavascriptStarxView Answer on Stackoverflow
Solution 2 - Javascriptgaby de wildeView Answer on Stackoverflow
Solution 3 - JavascriptJustin HelgersonView Answer on Stackoverflow
Solution 4 - JavascriptsbrbotView Answer on Stackoverflow
Solution 5 - JavascriptLuigi D'AmicoView Answer on Stackoverflow
Solution 6 - JavascriptKamil KiełczewskiView Answer on Stackoverflow
Solution 7 - JavascriptPrajakta KaleView Answer on Stackoverflow
Solution 8 - JavascriptNuclearPeonView Answer on Stackoverflow