EN
JavaScript - onclick event for input checkbox element example
6 points
In this article, we would like to show you how to handle the click event for the input checkbox element in JavaScript.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<label>
5
<input type="checkbox" onclick="onClick(this)" /> Agreement
6
</label>
7
<script>
8
9
function onClick(element) {
10
console.log('Agreement changed to ' + element.checked + ' by onclick event.');
11
}
12
13
</script>
14
</body>
15
</html>
xxxxxxxxxx
1
2
<html>
3
<body>
4
<label>
5
<input id="agreement" type="checkbox" /> Agreement
6
</label>
7
<script>
8
9
var agreement = document.getElementById('agreement');
10
11
agreement.onclick = function(element) {
12
console.log('Agreement changed to ' + agreement.checked + ' by onclick event.');
13
};
14
15
</script>
16
</body>
17
</html>
xxxxxxxxxx
1
2
<html>
3
<body>
4
<label>
5
<input id="agreement" type="checkbox" /> Agreement
6
</label>
7
<script>
8
9
var agreement = document.getElementById('agreement');
10
11
agreement.addEventListener('click', function(element) {
12
console.log('Agreement changed to ' + agreement.checked + ' by click event.');
13
});
14
15
</script>
16
</body>
17
</html>