Select All checkbox by Javascript or console

后端 未结 9 1299
野性不改
野性不改 2021-01-31 05:01

I am having 100 Checkboxes on my web page. For testing purposes I want to tick all those boxes, but manually clicking is time consuming. Is there a possible way to get them tick

9条回答
  •  清歌不尽
    2021-01-31 05:14

    The most direct way would be to grab all your inputs, filter just the checkboxes out, and set the checked property.

    var allInputs = document.getElementsByTagName("input");
    for (var i = 0, max = allInputs.length; i < max; i++){
        if (allInputs[i].type === 'checkbox')
            allInputs[i].checked = true;
    }
    

    If you happen to be using jQuery—and I'm not saying you should start just to tick all your checkboxes for testing—you could simply do

    $("input[type='checkbox']").prop("checked", true);
    

    or as Fabricio points out:

    $(":checkbox").prop("checked", true);
    

提交回复
热议问题