Skip to content Skip to sidebar Skip to footer

Javascript Toggle Checkbox

I need to toggle all buttons with a single function. Function needs to toggle all checkboxes in the document as my checkboxes are freestanding and not part of a form. I currently h

Solution 1:

Two main items to refactor. First, instead of doc it must be document. Second instead of relying on a global just pass in a boolean to determine whether or not to check the checkboxes.

functioncheckedAll(isChecked) {
    var c = document.querySelectorAll('input[type="checkbox"]');

    for (var i = 0; i < c.length; i++){
        c[i].checked = isChecked;
    }
}

JS Fiddle:http://jsfiddle.net/Jvnfm/107/

Solution 2:

You can alternatively perform the following for each checkbox element:

    c[i].click();

This version will trigger any associated event handlers associated with that element.

Post a Comment for "Javascript Toggle Checkbox"