Search code examples
javascripthtmldomcheckboxcss-selectors

Why isn't selecting a checkbox using document.querySelectorAll() working?


I have the following code, mostly from How can I select all checkboxes from a form using pure JavaScript and it's just not working.

Test.html

<html>
    <head>
    <script>
    function select(){
        var inputs = document.querySelectorAll("input[type='checkbox']");
        for(var i = 0; i < inputs.length; i++) {
            inputs[i].checked = true;   
        }
    }
    </script>
    </head>
    <body>
        <form id="myId" name="myForm">
        <input type="checkbox" value="1"/> 1
        <input type="checkbox" value="2"/> 2
        <input type="checkbox" value="3"/> 3
        <input type="button" onclick="select()"  value="Select all"/>
        </form>
    </body>
</html>

Clicking the button does nothing. I must be doing something really wrong here but I just can't pick it out.


Solution

  • Rename your function from select() to something else, e.g., onclickCheckbox().