javascriptcheckboxno-framework

Check/uncheck all checkboxes on without looping and without a framework


Is it possible to check or uncheck a set of checkboxes on a page a) without looping, and b) without using a Javascript framework such a jQuery?

This question is related but is about (un)checking all the checkboxes on a page with jQuery.

I expect the answer to my question will probably be "no", but if there's some weird, hacky way of doing it (not weird and not hacky is good too!) then I would like to know. Call it curiosity if you will.

Edit: I suppose what I'm really asking is for a way to do it in O(1) (constant time) rather than O(n) (linear time with respect to the number of checkboxes)


Solution

  • You may modify/override the full HTML code:

    <div id="checkBoxes">
    <input name="foo" type="checkbox" value="1" />
    <input name="bar" type="checkbox" value="1" />
    <input name="baz" type="checkbox" value="1" />
    </div>
    
    <script type="text/javascript">
    function checkAll(){
        document.getElementById("checkBoxes").innerHTML = 
             '<input name="foo" type="checkbox" value="1" checked="checked" />'
            +'<input name="bar" type="checkbox" value="1" checked="checked" />'
            +'<input name="baz" type="checkbox" value="1" checked="checked" />';
    }
    function uncheckAll(){
        document.getElementById("checkBoxes").innerHTML = 
             '<input name="foo" type="checkbox" value="1" />'
            +'<input name="bar" type="checkbox" value="1" />'
            +'<input name="baz" type="checkbox" value="1" />';
    }
    </script>
    

    no Loop, no Framework, just a little bit unesthetic..