HTML DOM Input Checkbox defaultChecked Property


The HTML DOM input checkbox defaultChecked property returns the default value of checked attribute of a checkbox in HTML.

Syntax

Following is the syntax −

object.defaultChecked

Example

Let us see an example of defaultChecked property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM checked property</title>
<style>
   body{
      text-align:center;
   }
   p{
      font-size:1.5rem;
      color:#ff8741;
   }
   input{
      width:30px;
      height:30px;
   }
   button{
      background-color:#db133a;
      color:#fff;
      padding:8px;
      border:none;
      width:180px;
      margin:0.5rem;
      border-radius:50px;
      outline:none;
      font-weight:bold;
   }
   .show-msg{
      color:#db133a;
      font-size:1.5rem;
      font-weight:bold;
   }
</style>
</head>
<body>
<h1>checked Property Example</h1>
<p>Are you smart?</p>
<input type="checkbox" checked>
<br>
<button onclick="check()">Lets Check? Click me</button>
<div class="show-msg"></div>
<script>
   function check() {
      var input = document.querySelector("input");
      var showMsg = document.querySelector(".show-msg");
      if(input.defaultChecked === true){
         showMsg.innerHTML="You are right!You are smart by default";
      } else {
         showMsg.innerHTML="Hmmmmm!You are not smart by default";
      }
   }
</script>
</body>
</html>

Output

This will produce the following output −

Click on “Lets Check? Click me” button to check whether the checkbox is by default checked or not?

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

260 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements