Select All Checkboxes By ID/Class

后端 未结 8 1883
孤城傲影
孤城傲影 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.

    0 讨论(0)
  • 2021-02-07 13:00

    JQuery is great when there's a pattern to match. See Check All Checkboxes with JQuery.

    0 讨论(0)
提交回复
热议问题