Cover Image for How to check a radio button using JavaScript
113 views

How to check a radio button using JavaScript

To check a radio button using JavaScript, you need to access the radio button element and set its checked property to true. Here’s an example of how you can achieve this:

JavaScript
// HTML:
// <input type="radio" id="radioButton" name="options" value="option1">
// <input type="radio" id="radioButton2" name="options" value="option2">

// JavaScript:
const radioButton = document.getElementById("radioButton");
radioButton.checked = true;

In the above example, we access the radio button element using getElementById and assign it to the radioButton variable. Then, we set the checked property of the radioButton to true to check the radio button programmatically.

Make sure to replace "radioButton" with the actual id of your radio button element. Additionally, note that radio buttons typically have a name attribute to define a group of related options. If you have multiple radio buttons with the same name, only one radio button within that group can be checked at a time.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS