<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
title
>
How to Check/Uncheck the
checkbox using JavaScript ?
</
title
>
</
head
>
<
body
>
<
h1
style
=
"color:green;"
>
GeeksforGeeks
</
h1
>
<
h2
>Check/Uncheck the checkbox using JavaScript</
h2
>
<
form
>
<
div
>
<
input
type
=
"button"
onclick
=
"checkAll()"
value
=
"Check All"
>
<
input
type
=
"button"
onclick
=
"uncheckAll()"
value
=
"Uncheck All"
>
</
div
>
<
input
type
=
"checkbox"
class
=
"check2"
>
<
label
>First</
label
>
<
input
type
=
"checkbox"
class
=
"check2"
>
<
label
>Second</
label
>
<
input
type
=
"checkbox"
class
=
"check2"
>
<
label
>Third</
label
>
</
form
>
<
script
>
// Create checkall function
function checkAll() {
let inputs = document.querySelectorAll('.check2');
for (let i = 0; i <
inputs.length
; i++) {
inputs[i]
.checked
=
true
;
}
}
// Create uncheckall function
function uncheckAll() {
let
inputs
=
document
.querySelectorAll('.check2');
for (let
i
=
0
; i < inputs.length; i++) {
inputs[i]
.checked
=
false
;
}
}
window.onload
=
function
() {
window.addEventListener('load', checkAll, false);
}
</script>
</
body
>
</
html
>