Sometimes, we want to get all checked checkboxes with JavaScript.
In this article, we’ll look at how to get all checked checkboxes with JavaScript.
How to get all checked checkboxes with JavaScript?
To get all checked checkboxes with JavaScript, we use querySelectorAll
.
For instance, we write
const checkedBoxes = document.querySelectorAll(
"input[name=mycheckboxes]:checked"
);
to call querySelectorAll
with "input[name=mycheckboxes]:checked"
to select all checkboxes with the name
attribute set to mycheckboxes
.
And we use :checked
to select the checked ones with the attribute.
Conclusion
To get all checked checkboxes with JavaScript, we use querySelectorAll
.