How to implement “select all” check box in HTML?

前端 未结 29 2869
悲哀的现实
悲哀的现实 2020-11-22 11:09

I have an HTML page with multiple checkboxes.

I need one more checkbox by the name \"select all\". When I select this checkbox all checkboxes in the HTML page must b

29条回答
  •  时光取名叫无心
    2020-11-22 11:45

    My simple solution allows to selectively select/deselect all checkboxes in a given portion of the form, while using different names for each checkbox, so that they can be easily recognized after the form is POSTed.

    Javascript:

    function setAllCheckboxes(divId, sourceCheckbox) {
        divElement = document.getElementById(divId);
        inputElements = divElement.getElementsByTagName('input');
        for (i = 0; i < inputElements.length; i++) {
            if (inputElements[i].type != 'checkbox')
                continue;
            inputElements[i].checked = sourceCheckbox.checked;
        }
    }
    

    HTML example:

    All of them

    Spacey, Kevin

    Firth, Colin

    Johansson, Scarlett

    I hope you like it!

提交回复
热议问题