How to get all checked checkbox values in JavaScript?

To get all checked checkboxes in JavaScript, do this: document.querySelectorAll('input[name=fieldName]:checked').

So if you have your checkboxes like this:

<input type="checkbox" name="myCheckboxes" id="treeCheckbox" value="tree"><label for="treeCheckbox">Tree</label>
<input type="checkbox" name="myCheckboxes" id="dogCheckbox" value="dog"><label for="dogCheckbox">Dog</label>
<input type="checkbox" name="myCheckboxes" id="catCheckbox" value="cat"><label for="catCheckbox">Cat</label>

And you check Dog and Cat.

Then you can use this JavaScript to get all checked checkboxes and their values:

const allChecked = document.querySelectorAll('input[name=myCheckboxes]:checked');

// NodeList(2) [input#dogCheckbox, input#catCheckbox]

// [ 'dog', 'cat' ]
console.log(Array.from(allChecked).map(checkbox => checkbox.value));