...

View Full Version : dynamically accessing form elements in loops



SnowBaby
10-16-2003, 01:22 PM
Ok I have a dynamically created form and need to do a verification check on it. I could write out a switch statment to do this however I'm starting to have long downloads simply because of the number of statements. I work mainly in .asp so I'm wondering if I can dynamically effect the form element I'm checking within the following loop. Each form element is named Student# with the # being a number from 1 to gsize.

in the example below I've put a # in where I'd need to effet the verification.

for (i = 1; i < gsize; i++){
if ( document.frmGroupSelect.Student#.value == "NotSelected" )
{alert ("Group member "+i+"has not been selected. Please complete the group"); return false;}}

Any help is apriciated. Thanx

Jon.

glenngv
10-16-2003, 02:32 PM
var f = document.frmGroupSelect;
for (i = 1; i < gsize; i++){
if ( f.elements['Student'+i].value == "NotSelected" )
{
alert ("Group member "+i+" has not been selected. Please complete the group");
return false;
}
}

I put the form reference to a variable outside the loop for efficiency...

But if the elements StudentN is a <select> element not a textbox, you need to do it this way for it to work cross-browser.

var f = document.frmGroupSelect;
for (i = 1; i < gsize; i++){
if ( f.elements['Student'+i].options[f.elements['Student'+i].selectedIndex].value == "NotSelected" )
{
alert ("Group member "+i+" has not been selected. Please complete the group");
return false;
}
}

I have a feeling it's a <select> by looking at the code. I could be wrong of course...:D

SnowBaby
10-16-2003, 02:38 PM
yea its a set of up to 10 dynamicaly created greated dropdown menus, which need to be selected and not repeated.

Nice. :D

Cheers for the help it works great, nicew idea with the form referance as a variable as well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum