Javascript DOM HTML Input Radio defaultChecked Property get

Introduction

Check if a radio button is checked by default:

var x = document.getElementById("myRadio").checked;

Click the button to find out if the radio button is checked by default.

View in separate window

<!DOCTYPE html>
<html>
<body>

Radio button: <input type="radio" name="colors" id="myRadio" checked>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {// www . jav a 2  s . c  om
  var x = document.getElementById("myRadio").checked;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The defaultChecked property returns the default value of the checked attribute.

This property returns true if the radio button is checked by default, otherwise it returns false.




PreviousNext

Related