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

using vars to address form elements

Status
Not open for further replies.

lazytrucker

Programmer
Aug 4, 2004
39
0
0
GB

Hello all, I have been trying to do this for some time now and with no success.

I have an onchange function which has to populate some form fields depending on what var is passed.

The function goes like this.

function func(val)
{
var call
call="document.fexp.a"+val; //val = 1
alert(call.value); //this returns undefined
alert(document.fexp.a1.value); //this returns the correct value
}

Is there any way to address form elements using the passed variable??
Any suggestions much appreciated

Cheers Lazy Trucker
 
Change the line

Code:
call="document.fexp.a"+val; //val = 1

to

Code:
call=document.getElementById("fexp.a"+val); //val = 1


Hope this helps.

John
 

Im afraid this doesn't work the value im trying to put into the alert() comes from a hidden text field, document.fexp.a1.value

if I give the element the same id (a1) it doesnt work.

Any other suggestions??
 
Try this:

alert(eval(call + ".value"));

*cLFlaVA
----------------------------
Breaking the habit...
 
You are trying to reference the "value" property of a string. What you want is the "value" property of a field.

The string contains this:
"document.fexp.a1"

the eval function will, well, evaluate the string. So, in my previous post, you're going to evaluate "document.fexp.a1.value" and then alert it.

Hope this helps.

*cLFlaVA
----------------------------
Breaking the habit...
 
Thanks for the replys I thought you were on the right track with the id's so I messed about a bit and got there in the end:

Function:

function func(val)
{
var call
call=document.getElementById("a"+val);
document.fexp.depapt.value=call.value;
}

Where the hidden field looks like:

<input type="hidden" id="<%="a"&i%>" value="<%=trim(rst("aspsql"))%>">

Hope this may help others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top