We can use JavaScript to check a radio button by making use of the getElementById() method along with targeting the checked property value. We simply need to change the checked property value of the radio button to true. Here is how we can do this:

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

Let’s see an example of this below.


Lets say we have the following HTML:

Select your favorite mode of transportation:

 





Set the radio button Airplane to checked

In this example, we will use the onclick event to call a function setRadio() which we will create below.

The setRadio() function will simply set the last radio button, Airplane, to be checked.

Here is our function using JavaScript:

function setRadio(){
  document.getElementById("airplane").checked = true;
}; 

Let’s try this out below:

Code Output:

Select your favorite mode of transportation:






Set the radio button Airplane to checked

Full Code:

Select your favorite mode of transportation:

 





Set the radio button Airplane to checked