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>
getElementByIdcan't find a particular element, it will returnnull. You cannot test.checkedon a null object... so it will crash. You need to test for whethergetElementByIdreturns a valid object or notmyCheckboxvsmyCheckbox0(see my answer). We need to see your HTML. Also, you never need== truein conditions. And you will need to have 10 checkboxes for that code not to throw an error.