# Slow Sums on Form

• Jan 11th, 2008, 11:33 AM
kalchas
Hi all,

I'm using the following script to some elements across rows in a form. Problem is that it really slows down entry of an order, as the form contains maybe a thousand different products. Here's the script:

Code:

```function sumQuantities() { i=0; while(i < document.orderForm.elements.length)     {     if((i%16==0)&&(i!=0))         {         var j = document.orderForm.elements[i-1].value-0;         var k = document.orderForm.elements[i-2].value-0;         var a = document.orderForm.elements[i-3].value-0;         var b = document.orderForm.elements[i-4].value-0;         var c = document.orderForm.elements[i-5].value-0;         var d = document.orderForm.elements[i-6].value-0;         var e = document.orderForm.elements[i-7].value-0;         var f = document.orderForm.elements[i-8].value-0;         var g = document.orderForm.elements[i-9].value-0;         var h = document.orderForm.elements[i-10].value-0;         var l = document.orderForm.elements[i-11].value-0;         var m = document.orderForm.elements[i-12].value-0;                 document.orderForm.elements[i-3].value = b + c + d + e + f + g + h + l + m;         document.orderForm.elements[i-1].value = tidy((b + c + d + e + f + g + h + l + m)*k);         }     i++;     } }```
Any ideas about how I could speed things up?

Cheers,

K.
• Jan 11th, 2008, 12:38 PM
hyperair
Use parseInt() to convert a string into an integer, and parseFloat() to convert a string into a floating point number. In your case:

Code:

`var j = parseInt(document.orderForm.elements[i-1].value);`
And repeat the change for the rest of them.

Also, could you show what's the contents of your tidy() function? Perhaps optimizations can be done there as well.

Another thing you could do is save the form's elements to a variable. Then you won't have to refer through document so often.

Code:

```var elms = document.orderForm.elements; var j = parseInt(elms[i-1].value); ...```
• Jan 11th, 2008, 03:22 PM
kalchas
Hi Hyperair,

Thanks for your help. The use of a variable for the form elements seems to help a bit. I'm having some trouble, though, getting the parseInt function to work. I'm consistently getting an NaN error when I use it. Any ideas on why that might be?

Cheers,

K.
• Jan 11th, 2008, 10:45 PM
hyperair
Whoops sorry I forgot that parseInt cannot handle blank strings or strings with utterly no numbers in them. You'll have to do a check for blank strings and change them to 0. I reckon it could end up taking more CPU so if it does you should just go back to the old. However, I reckon it would be worth testing.
• Feb 14th, 2008, 05:29 PM
kalchas
Hi all,

Just wanted to report back. I've got it set up so that we start with a value of 0 for all inputs before the customer starts entering their order, and that's resolved the parseInt issue. The code I'm using runs like this:

Code:

```var elms = document.orderForm.elements; var elmslength = elms.length; function sumUnits()     {     for( i=1; i<elmslength; i++)         {         if((i%15==0))             {             var a = parseFloat(elms[i-12].value);             var b = parseFloat(elms[i-11].value);             var c = parseFloat(elms[i-10].value);             var d = parseFloat(elms[i-9].value);             var e = parseFloat(elms[i-8].value);             var f = parseFloat(elms[i-7].value);             var g = parseFloat(elms[i-6].value);             var h = parseFloat(elms[i-5].value);             var k = parseFloat(elms[i-4].value);                             elms[i-3].value = a + b + c + d + e + f + g + h + k;             elms[i-1].value = parseFloat(elms[i-3].value) * parseFloat(elms[i-2].value);             }         }     }```
I'm declaring the elms and elmslength variables outside the function, because I'm reusing them in my validation functions.

Cheers,

K.