Select All Checkboxes By ID/Class

后端 未结 8 1893
孤城傲影
孤城傲影 2021-02-07 12:27

I\'m really bad at Javascript and I\'m struggling to get my head round it.

What I\'m trying to do is get something to select all checkboxes. However everything I have fo

8条回答
  •  天涯浪人
    2021-02-07 12:59

    Pseudo code:

    function checkAll(array_with_id_values)
    {
        for (var i = 0, ilen = array_with_id_values.length; i < ilen; i++)
        {
            document.getElementById(array_with_id_values[i]).checked = true;
        }
    }
    

    Call it with:

    checkAll(['my-unique-id', 'my-other-unique-id', 'my-third-unique-id']);
    

    You should ofc not use such verbose variable names and do checks that the argument is an array and that the element you get is a checkbox etc etc.

提交回复
热议问题