What method would be best to use to selectively set a single or multiple radio button(s) to a desired setting with JavaScript?
-
1@Starx - jQuery took care of my most recent rash; surely it can check radio buttons!Justin Helgerson– Justin Helgerson2012-02-28 05:02:32 +00:00Commented Feb 28, 2012 at 5:02
-
9@RintoGeorge, I agree, i love jQuery too, but this is too simple. We dont need jQuery for this.Starx– Starx2012-02-28 05:04:52 +00:00Commented Feb 28, 2012 at 5:04
9 Answers
Very simple
radiobtn = document.getElementById("theid");
radiobtn.checked = true;
5 Comments
radiobtn.checked should receive 'checked' instead of true, although i can't remember where i read this..checked with the xhtml-attribute checked="checked". Since xhtml tried to be xml-compliant, no attributes without value were allowed (in contrast to html 4 or 5 where <sometag checked> is correct syntax). Regardless of which mark-up language you're using, Javascript is Javascript and in Javascript objects have properties which could very well be a boolean value. Indeed, the DOM object associated with <input type="radio" /> has a property .checked which can be true or false.var before radiobtn = ... to avoid having it in the global scope.onclick="theid.checked=true" in the input tag of the activating button.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/
Comments
You can also explicitly set value of radio button with the following script:
document.gendersForm.gender.value = "F";
<form name="gendersForm">
<input type="radio" name="gender" value="M" /> Man
<input type="radio" name="gender" value="F" /> Woman
</form>
and corresponding radio button will be checked automatically.
1 Comment
Vanilla Javascript:
yourRadioButton.checked = true;
jQuery:
$('input[name=foo]').prop('checked', true);
or
$("input:checkbox").val() == "true"
7 Comments
[0], [1], [2], etc. after the $(...) part of it to do this before, but when I just tried $('input[name=foo]')[0].attr('checked', true); I got Object doesn't support this property or method. Same thing if I put ('checked', 'checked'). But when I mixed the JavaScript with the jQuery option and did $('input[name=foo]')[0].checked = true; it set just fine. Odd./**
* 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;
}
}
}
Comments
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");
2 Comments
radiobtn.setAttribute("checked", ""); to select it.$("#id_of_radiobutton").prop("checked", true);
2 Comments
This sets checked using name to cycle through the elements and a value check to set the desired element to true. I kept it as simple as possible, its pretty easy to put it into a function or a loop, etc.
variable 'nameValue' is the radio elements name value
variable 'value' when matched sets the radio button
Array.from( document.querySelectorAll('[name="' + nameValue + '"]') ).forEach((element,index) =>
{
if (value === element.value) {
element.checked = true;
} else {
element.checked = false;
}
});