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.

3 Answers 3

5

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

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

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

Sign up to request clarification or add additional context in comments.

Comments

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;
        }
    }

Comments

-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?

4 Comments

Its seen as a bit of a cardinal sin round here to answer a question not taged jQuery with a jQuery answer.
There is JavaScript outside of jQuery
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.
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 ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.