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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using dynamically named text fields 1

Status
Not open for further replies.

DanT07

Programmer
May 11, 2005
38
GB
Hi there, beginners question i think.

I have an asp page which is creating varying numbers of rows depending on the sql statement. Each row has 6 textfields going across. These text fields are named dynamically using a result pulled from the database. For example the first text field is defined:

< input id="<%response.write(rs("Stock_Code"))%>" >

The next field along in the row, so it has a different ID, I have defined in this way:

< input id="<%response.write(rs("Stock_Code"))%>_actual" >

And this continues along the row. So I want my javascript to change the values of the 5 fields after the first one is altered so I use this:

onChange="updateAverageCost(this.id)" on the first text field.

My problem is in the script how do i refer to the other text fields in that particular row? I have tried the following script and many other similar attempts but i cant manage it.

function updateAverageCost(fieldID) {
var average2 = fieldID + "_average";
var actual2 = fieldID + "_actual";
var averagecost = document.form1.average2.value;
var actualcost = document.form1.actual2.value;
document.write(actualcost);

Anyway thanks in advance,
Dan
 
A couple of approaches could be used. You could dynamically generate the Javascript code from the ASP script and write the field names into the Javascript similar to the way you generate the form field names.

Or, you could use the eval() function in Javascript to construct a string to be executed in the Javascript code. Like so
Code:
. . .
var averagecost = eval( "document.form1." + average2 + ".value");
. . .
 
[tt]>var averagecost = document.form1.average2.value;
var averagecost = document.form1.[red]elements[average2][/red].value;
>var actualcost = document.form1.actual2.value;
var actualcost = document.form1.[red]elements[actual2][/red].value;[/tt]

But, this won't do any good. Where do you want to write to?
[tt]>document.write(actualcost);[/tt]
 
Both of those methods give an error.

The first:

var averagecost = eval( "document.form1." + average2 + ".value");

gives an 'Expected Identifier' Error on that line ^^^

The second:

var actualcost = document.form1.elements[actual2].value;

gives 'document.form1.elements[actual2].value null or not a value'
 
>gives 'document.form1.elements[actual2].value null or not a value'
So, document.form1.elements[average2].value is a value. What do you say if elements[actual2].value null or not a value? Maybe that is a purely id approach.
[tt]
>var averagecost = document.form1.average2.value;
var averagecost=document.getElementById(avarage2).value;
>var actualcost = document.form1.actual2.value;
var actualcost=document.getElementById(actual2).value
[/tt]
 
That work yes.

I will work on that approach and see if i can get the rest of what i want to happen working.

Thanks very much
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top