Click to See Complete Forum and Search --> : document.form.variable.value


Arc
05-14-2003, 08:01 PM
Hi, I have a peice of code that i need to loop through some dynamically created file buttons.

I have no idea how many buttons will be added to the form but they are all named in sequential order i.e. Image0,Image1,Image2,ect...

So what i am trying to do is create a validation script to check that all of these file buttons have a value. I am using a For Loop looping through as many times as there are file buttons. I know how many because they entered it on the last page and the variable is stored in a hidden input box.

The problem is document.form doesnt seem to accpet a variable as a control name even though the variable has the same value as the name of the control. Example:

var TheImage = "Image"+Counter;
if(document.form.TheImage.value == "")

The variable 'TheImage' holds the Value Image0, but the script doesnt work when the variable is in place of the control name. If i manualy enter the control name like so

if(document.form.Image0.value == "")

then the script works.

So how do i use a variable as a control name to validate my fields? Or is there another way to validate the fields?

Thanks!:D

AdamBrill
05-14-2003, 09:15 PM
Something like this should do:count=0;
while(eval("document.formName.image"+count)){
alert(eval("document.formName.image"+count+".value"));
count++;
}

Arc
05-14-2003, 09:56 PM
yah thanks dave. i had figured that out a bit ago and updated the title of this thread to [Resolved] but apparently this forum doesnt support that.

But thanks any way guys!:D

bloke
09-17-2003, 09:27 AM
Guys, I'm having a similar problem which I have posted here:

http://forums.webdeveloper.com/showthread.php?s=&postid=92657#post92657

(Sorry, I didn't see this post before I posted that one!)

I have tried several variations, all with no success. The general gist of it is:


for(i=1; i<19; i++) {
var thing = "Item"+i
var something = "Quantity"+i
// alert(thing)
// alert(something)


if ((document.frm.thing.value != 0) && (document.frm.something.value == 0))
{
alert("Please enter a quantity")
document.frm.something.focus()
return false
}
}


also tried a few variations of this:


if ((eval("document.frm."+thing+".value") != 0) && ((eval("document.frm."+something+".value") == 0))


Desperation has set in!

Cheers

Arc
09-17-2003, 12:33 PM
To get the value of a variable you simply call the variable name.


if (thing != 0 && something == 0){

blah
}

bloke
09-18-2003, 05:59 AM
Sorted it last nite, but thanx anyway! :D