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:
xxxxxxxxxx
1
var checkbox = document.getElementById('my-checkbox');
2
var checked = checkbox.checked;
More detailed description of solution is placed below.
In this section checked
property is used to get current state of checkbox.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<label>
5
<input id="my-checkbox" type="checkbox" /> Agreement
6
</label>
7
<script>
8
9
var checkbox = document.querySelector('#my-checkbox');
10
11
checkbox.addEventListener('click', function() {
12
console.log(checkbox.checked); // or this.checked
13
});
14
15
</script>
16
</body>
17
</html>