[thelist] what is wrong with this JS function?

Erik Mattheis gozz at gozz.com
Thu Mar 6 18:28:01 CST 2003


On Thursday, March 6, 2003, at 05:45  PM, Chris W. Parker wrote:
> 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"
>
> 1. Why is it doing this?

Because you're including the text input too. You want to say:

  if (currentForm.elements[iCtr].type == 'select-one') {
   outputText += currentForm.elements[iCtr].value;
  }

> 2. How can I access the last element?

currentForm.elements[currentForm.elements.length - 1]

PS, you might want to change your corresponding line to this:

for(iCtr=0;iCtr<currentForm.elements.length;iCtr++)

I'm not sure why, but it feels right to me.
-----------------------
Erik Mattheis
GoZz Digital
<http://goZz.com/>
Flash and ColdFusion Development
Minneapolis, MN
-----------------------




More information about the thelist mailing list