I have a simple function which if somone selects an option from a drop down list, populates the selected value into a text box, or allows the user to type a value into the text box. The script works fine, if I use the name of the select box and text box in the function, if I try to use variables it won’t work. As I have multiple pairs of boxes I only want to write the function once, and send the box names as variables, but I can’t get it to work. Where am I going wrong?
This is my original code:
function insertCustName()
{
document.form1.CUSTOMER.value=document.form1.CustNameList.options[document.form1.CustNameList.value].text
}
function resetCustNameList()
{
document.form1.CustNameList.value=document.form1.CustNameList.options[0].text
}
<SELECT NAME=CustNameList onchange=”insertCustName()”>
<OPTION VALUE=0 SELECTED></OPTION>
<OPTION VALUE=1>Customer 1</OPTION>
<OPTION VALUE=2>Customer 2</OPTION>
</SELECT>
<INPUT TYPE=”text” NAME=”CUSTOMER” SIZE=15
onChange=”javascript:this.value=this.value.replace(‘&’,’+’);this.value=this.value.toUpperCase();resetCustNameList()”>
I thought this would work, but it doesn’t
function insertName(Value1,Value2)
{
document.form1.Value1.value=document.form1.Value2.options[document.form1.Value2.value].text
}
function resetList(Value3)
{
document.form1.Value3.value=document.form1.Value3.options[0].text
}
<SELECT NAME=CustNameList onchange=”insertName(CUSTOMER,CustNameList)”>
<OPTION VALUE=0 SELECTED></OPTION>
<OPTION VALUE=1>Customer 1</OPTION>
<OPTION VALUE=2>Customer 2</OPTION>
</SELECT>
<INPUT TYPE=”text” NAME=”CUSTOMER” SIZE=15
onChange=”javascript:this.value=this.value.replace(‘&’,’+’);this.value=this.value.toUpperCase();resetList(CustNameList)”>