Forum Moderators: open
I've got this function to disable a couple dozen checkboxes when another one is checked. It all worked well till I decided to get smart and throw a for loop into it, now it ..well, doesn't.
here it is..
function killme() { //called later with OnClick
if(document.form.nobeer.checked)
{
document.form.anybeer.disabled=true; //this works
document.form.anybeer.checked=false; //this works
document.form.otherbeer.disabled=true; //this too
document.form.otherbeer.value=''; //so does this..
for(x=1; x<=25; x++){ //but my loop
document.form.beer[x].checked=false; //doesn't
document.form.beer[x].disabled=true; //work
}
}
else
{
document.form.anybeer.disabled=false;
document.form.otherbeer.disabled=false;
for(x=1; x<=25; x++){ //neither does
document.form.beer[x].disabled=false; //this one
}
}
}
//---------------------
beer[] is an array containing all of the checkbox values.
anybeer and nobeer are checkboxes.
otherbeer is a text box.
any ideas?
beer[] is an array containing all of the checkbox values.
In that case,
beer[n] will be a string (not checkbox), and will not have a checked property. ..or do you mean that you have a number of checkboxes with name,
beer in the form? In which case beer will be a collection [edited by: Bernard_Marx at 10:53 am (utc) on Oct. 11, 2004]
During my modifications, I added an 'id' tag to the checkboxes so I could use 'label for='
apparently in this case, javascript uses the 'id' instead of the 'name'.
Here's one of the checkboxes:
<input type="checkbox" name="beer[1]" id="beer1" value="yes"><label for="beer1">Molson Canadian</label>
...so I just had to get rid of the [ ] in the function:
...document.form.beer[x].disabled=true;...
all works well now.