Vignesh Krishnamoorthy
Vignesh Krishnamoorthy

Reputation: 142

Radio button trigger with Javascript

I have two radio buttons and I need first radio button to be check after window load with Javascript. By using its class automatically need to be triggered

Upvotes: 0

Views: 4136

Answers (2)

Mamun
Mamun

Reputation: 68933

Try the following way:

function manageRadioButton() {
  var x = document.getElementsByClassName("radio");
  x[0].checked = true;
}
<body onload="manageRadioButton()">
  <input type="radio" class="radio" name="gender" value="male"> Male<br>
  <input type="radio" class="radio" name="gender" value="female"> Female<br>
</body>

Upvotes: 2

edcs
edcs

Reputation: 3879

Adding this JavaScript to the body of your page will trigger a self executing function when the page loads and will check the radio button. You can add the extra logic you mentioned into the function.

<input type="radio" name="radioButton" id="radioButton_1" value="">

<script>
    (function() {
        document.getElementById("radioButton_1").checked = true;
    })();
</script>

Upvotes: 1

Related Questions