Skip to content

On page load check a radio button based on its id using Javascript

I want on page load to check a radio button by default using pure javascript. Here is my code:

window.onload = function() {
var radioman = document.getElementById("btn-Man").value;
radioman.checked = true;
<input required="required" type="radio" id="btn-Man" name="gender" value="1" class="radio-class">
<label for="btn-Man">Man</label>

<input required="required" type="radio" id="btn-Woman" name="gender" value="2" class="radio-class">
<label for="btn-Woman">Woman</label>

but it doesn’t work. On page load the first radio button with the id=”btn-Man” should be checked. What in the world i ‘m doing wrong ?



Try without ‘.value’ like:

var radioman = document.getElementById("btn-Man");
radioman.checked = true;