Javascript DOM HTML Input Checkbox checked Property set

Introduction

Set the checked state of a checkbox:

View in separate window

<!DOCTYPE html>
<html>
<body>

Checkbox: <input type="checkbox" id="myCheck">

<button onclick="check()">Check Checkbox</button>
<button onclick="uncheck()">Uncheck Checkbox</button>

<script>
function check() {//from w w  w. j ava 2 s  .  c o m
  document.getElementById("myCheck").checked = true;
}

function uncheck() {
  document.getElementById("myCheck").checked = false;
}
</script>

</body>
</html>

The checked property sets or gets the checked state of a checkbox.

This property mirrors the HTML checked attribute.

The checked property accepts and returns a boolean value.

Value Description
true The checkbox is checked
false Default. The checkbox is not checked

It returns true if the checkbox is checked, and false if the checkbox is not checked




PreviousNext

Related