EN
JavaScript - how to check if input checkbox is checked?
9
points
In this article, we're going to have a look at how to check if checkbox is checked in Pure JavaScript.
Quick solution:
var checkbox = document.getElementById('my-checkbox');
var checked = checkbox.checked;
More detailed description of solution is placed below.
1. Vanilla JavaScript example
In this section checked
property is used to get current state of checkbox.
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<label>
<input id="my-checkbox" type="checkbox" /> Agreement
</label>
<script>
var checkbox = document.querySelector('#my-checkbox');
checkbox.addEventListener('click', function() {
console.log(checkbox.checked); // or this.checked
});
</script>
</body>
</html>