Page 1 of 1

Using variables when referring to form elements using JS

Posted: Tue Apr 08, 2008 10:16 am
by Starcraftmazter
Hey guys.

I'm using javascript to loop through all the input textboxes of a form and sent them to nothing. I need to have a loop variable called $i in the name of the input element, however I can't find a way to do that (syntax errors, etc), so I am using the ID of the element - which is ok, but out of curiosity I wonder if it's possible.

Code: Select all

 
        for ($i = 0; $i < 16; $i++) {
            // This doesn't work, because I can't find a way to insert $i into the LHS. 
//         document.program.addr_+$i.value = '';
 
            // This works fine
            document.getElementById("addr_"+$i).value = '';
        }    
 

Thanks!