Skip to main content

How to read the values from multiple checkboxes in JavaScript

· One min read
CTO

If you have a form, and on a form submission you need to get the values of all checkboxes that were ticked, you can use the following code.

document.querySelector("#my-form").addEventListener("submit", async (event) => {
event.preventDefault();

const checkboxes = event.target.querySelectorAll(
'input[name="counsellingFormat"]:checked'
);
const counsellingFormats = [];
for (const checkbox of checkboxes) {
counsellingFormats.push(checkbox.value);
}

console.log(counsellingFormats);
});

You can play with the code here.