How to check a radio button with JavaScript?

Spread the love

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 set the 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 radio buttons.

Then we write

document.getElementById("_1234").checked = true;

to select the radio button with getElementById.

And we set the checked property to true to check the radio button.

Conclusion

To check a radio button with JavaScript, we set the checked property to true.

Leave a Reply

Your email address will not be published. Required fields are marked *