Forum Moderators: open

Message Too Old, No Replies

more checkbox issues

for loop within function

         

oilfield

4:42 am on Oct 11, 2004 (gmt 0)

10+ Year Member



Hi all.
I'm a hack. at best.
I barely started to wrap my brain around a bit of PHP, and I found a need to learn a bit of JS...

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?

Bernard Marx

10:49 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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
...and I can't yet see why it wouldn't work.

[edited by: Bernard_Marx at 10:53 am (utc) on Oct. 11, 2004]

Bernard Marx

10:49 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<duplicate>

Rambo Tribble

2:11 pm on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The origins of the beer[] array are a fundamental element in divining the problem. Then again, perhaps a higher power is trying to convey the message that "otherbeer" should never, ever be disabled (though, clearly, this is not the essential level of sacrilege implicit in the "nobeer" category, to begin with).

Bernard Marx

3:09 pm on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's just the beer talking.

oilfield

9:11 pm on Oct 11, 2004 (gmt 0)

10+ Year Member



thanks guys... figured it out.

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.

Rambo Tribble

10:28 pm on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think, perhaps, your problem stems from a difference in how PHP and JavaScript handle names with brackets. JavaScript does not automatically create an array from elements so endowed.