Javascript Reference - HTML DOM Input Checkbox defaultChecked Property








This defaultChecked property returns true if the checkbox is checked by default, otherwise it returns false.

Browser Support

defaultChecked Yes Yes Yes Yes Yes

Syntax

var v = checkboxObject.defaultChecked 

Return Value

A Boolean type value, true if the checkbox is checked by default, otherwise it returns false.





Example

The following code shows how to check if an checkbox is checked by default.


<!DOCTYPE html>
<html>
<body>
<!--from ww  w  .  j  a va  2s  .  c  om-->
Checkbox:<input type="checkbox" id="myCheck" checked>
<button type="button" onclick="myFunction()">test</button>

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

<script>
function myFunction() {
    var x = document.getElementById("myCheck").defaultChecked;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: