[thelist] what is wrong with this JS function?

Chris W. Parker cparker at swatgear.com
Thu Mar 6 17:43:01 CST 2003


Hi.

I am trying to concatenate the values of some drop-down boxes into a
string and have that string be the value of a text box in the same form.
For example...

<form id="form1">
<select name="field1" onchange="showPartNumber(document.forms[1])">
 <option value="aa">AA</option>
 <option value="bb">BB</option>
</select>

<select name="field2" onchange="showPartNumber(document.forms[1])">
 <option value="cc">CC</option>
 <option value="dd">DD</option>
</select>

<input type="text" name="text1" size="40">
</form>

In the default state the text field should be "". When you choose 'AA'
from the first drop down the text field should instantly change to "AA".
When you choose 'DD' from the second drop down the text box should
change to "AADD".

I had this working at one point because I hard coded the function that
builds the string. But as I went along I found out that I needed to make
this function dynamic. (The reason for that is because I have three
forms on one page and one of the forms has a different number of drop
down fields.)

Here is the function I have so far...

function showPartNumber(currentForm)
{
 var outputText="";

 for(iCtr=0;iCtr<currentForm.length;iCtr++)
 {
  outputText += currentForm.elements[iCtr].value;
 }

 currentForm.elements[iCtr-1].value = outputText;
}

With this function I get these results:

Default: ""
1st change: "AA"
2nd change: "AADDAA"


So now for two quustions...

1. Why is it doing this? I thought by reinitializing the outputText
variable each time the function was fired it would get rid of this
problem, but it didn't. :(
2. How can I access the last element? (For fun I tried
elements[lastIndex], but that didn't work.)


thanks,
chris.



More information about the thelist mailing list