Sometimes, we want to check a radio button with JavaScript.
In this article, we’ll look at how to check a radio button with JavaScript.
How to check a radio button with JavaScript?
To check a radio button with JavaScript, we can set the checkbox’s checked
property to true
.
For instance, we write
<input type="radio" name="main-categories" id="_1234" value="1234" />
<input type="radio" name="main-categories" id="_2345" value="2345" />
<input type="radio" name="main-categories" id="_3456" value="3456" />
<input type="radio" name="main-categories" id="_4567" value="4567" />
to add 4 radio buttons.
Then we write
document.getElementById("_1234").checked = true;
to select the checkbox we want to check with getElementById
.
And we set its checked
property to true
to check it.
Conclusion
To check a radio button with JavaScript, we can set the checkbox’s checked
property to true
.