-2

I am trying to select all the checkboxes that have a specific class but I am not successful.

I have the following code:

function toggle(source) {
    var checkboxes = document.querySelector('.checkbox');
    for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i] != source)
            checkboxes[i].checked = source.checked;
    }
}
<input type="checkbox" onclick="toggle(this);" />Check all?<br />

<input type="checkbox" />Bar 1<br />
<input type="checkbox" class="checkbox" />Bar 2<br />
<input type="checkbox" class="checkbox" />Bar 3<br />
<input type="checkbox" />Bar 4<br />

They work correctly with querySelectorAll('input[type="checkbox"]') but it is not what I am looking for, I just want to mark the checkboxes with the "checkbox" class

Maria
  • 7
  • 2

0 Answers0