Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Variables in document string

Status
Not open for further replies.

jkelly295

MIS
Aug 14, 2001
15
US
I have the following bit of code

for (var i=0; i<numLines; i++)
{
alert (document.form1.TQuantity+i.value);
fieldQty = fieldQty + document.form1.TQuantity+i.value;
}

The alert box lists the value as object[undefined]. Is there a way to include variables in this way..ie

document.form1.TQuantity0.value
document.form1.TQuantity1.value
....
..
.

 
You can't access an element of a form by adding a varible to it like this:
[tt]document.form1.TQuantity+i.value[/tt]

I think you confusing it with arrays where you would do it like this:
[tt]document.form1.TQuantity
Code:
[i]
.value[/tt]


Even then it won't work in this case because it's not an array. The only way to access it enter the whole name:
[tt]document.form1.TQuantity1.value[/tt]

The form object does have an array called elements which contains all the items in that form. You can use an array to loop through all the elements and then check to see if the name of the elemets matches the name your looking for.
Try this:

[tt]
fieldQty = &quot;&quot;;

//Loop through all the elements in the form
for (var i=0; i < document.form1.elements.length; i++)
{
//Retrive the next element from the form
TQuanityX = document.form1.elements;

//Check to ensure that the name of the element starts out
if (TQuanityX.name.toString().substr(0,9 ))
{
alert (TQuanityX.value);
fieldQty = fieldQty + TQuanityX.value;
}
}
[/tt]
 
OOPS I messed this line up:
[tt]if (TQuanityX.name.toString().substr(0,9 ))[/tt]
It Should be:
[tt]if (TQuanityX.name.toString().substr(0,9 ) == &quot;TQuantity&quot;)[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top