2

I have the following HTML:

<form name="smform" id="smform" method="post" action="">
      <input type="checkbox" name="Check_All" onClick="Check(document.smform.check)"  >

     <input type="checkbox" name="check" value="blue_color"> Blue<br>
     <input type="checkbox" name="check" value="green_color"> Green<br>
     <input type="checkbox" name="check" value="orange_color"> Orange<br>
</form>

With the this JS:

function Check(chk)
    {
        if(document.smform.Check_All.checked){
        for (i = 0; i < chk.length; i++)
        chk[i].checked = true ;
        document.smform.Check_All.checked = true;
    }else{

        for (i = 0; i < chk.length; i++)
        chk[i].checked = false ;
        document.smform.Check_All.checked = false;
    }
}

And this this is working fine, I can select or deselect all checkboxes, but in order to post this as an ARRAY, my check boxes will have the name="check[]" like:

     <input type="checkbox" name="check[]" value="blue_color"> Blue<br>
     <input type="checkbox" name="check[]" value="green_color"> Green<br>
     <input type="checkbox" name="check[]" value="orange_color"> Orange<br>

But in this case my JS is not working. How can I adapt this JS so it works with name="check[]" ?

All help is appreciated.

Adnan
  • 25,882
  • 18
  • 81
  • 110

3 Answers3

5

Just dont use dot-notation, instead use square brackets and a string:

onClick="Check(document.smform['check[]'])"

Live example: http://jsfiddle.net/5JCMS/

Jamiec
  • 133,658
  • 13
  • 134
  • 193
2

you can use getElementsByTagName to get all checkboxes

var inputs = document.getElementsByTagName("input");
Check(inputs);
    function Check(chk)
        {
            if(document.smform.Check_All.checked){
              for (i = 0; i < chk.length; i++){
                if(inputs[i].type == "checkbox"){
                    chk[i].checked = true ;
                }
               }
            document.smform.Check_All.checked = true;
        }else{

            for (i = 0; i < chk.length; i++){
               if(inputs[i].type == "checkbox"){
            chk[i].checked = false ;
             }
           }
            document.smform.Check_All.checked = false;
        }
    }
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
-3

Use Jquery. You can add a class to all the checkboxes you wish to check

<input type='checkbox' class='autocheckable someOtherClass' />Blue

And then do as described here: Setting "checked" for a checkbox with jQuery?

Community
  • 1
  • 1
immutabl
  • 6,857
  • 13
  • 45
  • 76
  • 1
    Its seen as a bit of a cardinal sin round here to answer a question not taged `jQuery` with a jQuery answer. – Jamiec Oct 14 '11 at 14:51
  • 1
    There is JavaScript outside of jQuery – Matt Oct 14 '11 at 14:52
  • Even though, this gives an idea/suggests using classes in standard javascript and then calling the class to check the checkboxes, the OP can use this without jQuery. – jackJoe Oct 14 '11 at 14:54
  • 3 downvotes! Sheeesh. Well Ok. I assume the poster is learning - no one coding for money in the real-world would do this without JQuery ;-) – immutabl Oct 17 '11 at 08:25