Forum Moderators: open

Message Too Old, No Replies

toggle disable on input elements

         

lbombardier

2:31 pm on Aug 23, 2009 (gmt 0)

10+ Year Member



I have a set of 10 input boxes which all have the name & id in the format mybox[1], mybox[2] etc. By default they are set to disabled.

I have a control checkbox which on click class my toggle function. The issue is that I can't seem to access the mybox[] elements using a variable in a for loop. eg.

function toggleMe()
{
if(document.addRound.enableMybox.checked)
{
for(i=0; i < 10; i++)
{
adj = i + 1;
document.addRound.elements[mybox[adj]].disabled=false;
}
}
}

I get the alert that document.addRound.elements[mybox[adj]] is undefined. I've tried a number of different ways to access the element but nothing works. As a note, it does work if I put the variable in manually ie.:

document.addRound.elements['mybox[1]'].disabled=false;

I mention this to demonstrate that there are no naming issues up to the point of accessing the element.

Any help would be appreciated.

astupidname

4:31 pm on Aug 23, 2009 (gmt 0)

10+ Year Member



Based on your description, saying that: elements['mybox[1]'] works, then the following should work I would think, you are mainly forgetting to quote mybox and treat as string:
function toggleMe()
{
if(document.addRound.enableMybox.checked)
{
for(var str, i=0; i < 10; i++)
{
str = 'mybox['+ (i + 1) +'];
document.addRound.elements[str].disabled=false;
}
}
}

astupidname

4:33 pm on Aug 23, 2009 (gmt 0)

10+ Year Member



I forgot a quote mark at the end of this line there, should be: str = 'mybox['+ (i + 1) +']';