Marcus Tan
Marcus Tan

Reputation: 417

Using for loop to populate array in document.getelementbyid

i now facing a problem with using looping on my project. Currently i have a multiple checkbox which have their own unique id example :myCheckbox,myCheckBox1,myCheckBox2,....

I don't wish to hardcore the number behind the myCheckBox, so i try to use a for loop to get it done. Somehow i dono why my for loop does work, if i hardcode it like document.getelementbyid('mycheckbox1').checked==true then it work.

Anything i miss out in my code? Please guide me through as i'm still new to web programming. Thanks

Here is my code:

<script type='text/javascript'>

        function checkDisabled(yourSubmitButton){

          for(var i=0;i<10;i++){
              if(document.getElementById("myCheckBox"+i).checked==true){
                  yourSubmitButton.disabled = false;
                    return; 
              }
          } 


            // If we have made it here, disable it
            yourSubmitButton.disabled = true;

          };
          </script> 

Upvotes: 0

Views: 119

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074355

Stab in the dark: You've said in your question that your checkboxes are called myCheckbox, myCheckbox1, myCheckBox2, and such, but your loop is looking for myCheckbox0, not myCheckbox, on the first iteration, which would make it throw an exception when you try to read the checked property of null.

Upvotes: 2

Related Questions