# need help with var's

Go to solution Solved by nogray,

## Recommended Posts

Ok so I've watch a ton of tutorials, read a few books asked a whole bunch of questions and still can't find a solution. Here is my problem,

I have this form here : http://jemtechnv.com/beta/finish_ticket.php  if you play with the form you will see that entering input in the Qty, and Price boxes will populate the Ext box along with sales tax and material cost and Material boxes, and input in the rate and hours box will populate the subtotal and labor boxes now here is my problem. I need to auto populate the total hours box by from the sum of the hours column and I also need to sum up the material and labor boxes and populate that total in the grand total box, sounds easy huh? you would think but here is my code that I use to calculate my sales tax :

```function tax(){
var materialcost = document.getElementById( 'materialcost' ).value;
var shipping = document.getElementById( 'shipping' ).value;
var salestax = Math.round(((materialcost / 100) * 8.1)*100)/100;
var materialtotal = (materialcost * 1) + (salestax * 1) + (shipping * 1);

document.getElementById( 'materialcost' ).value = materialcost;
document.getElementById( 'salestax' ).value = salestax;
document.getElementById( 'shipping' ).value = shipping;
document.getElementById( 'materialtotal' ).value = materialtotal;

}
```

this works fine but when I try to use the materialtotal in another function to total up the materialtotal and labortotals for the grand total it causes the browser to lock up. Can someone tell me how I can get my grand total without making the script too complicated? Thanks!

##### Share on other sites

Functions shouldn't affect global variables.

You can define the variables outside of the functions and pass a reference to the variables inside of the function.

##### Share on other sites

I've tried that and it locks up the browser. Any 2nd reference to the materialtotal field immediately locks up the browser when the script loads, it's almost as if it's stuck in an indefinite loop and causes the browser to stick at that point. If I remove the 2nd reference to materialtotal of the 2nd function the browser doesn't lock up but then I don't get the desired result!

Edited by achesnpains
##### Share on other sites

• Solution

You can simply ready the input value for the total in your other function, e.g.

```function my_other_function(){
var materialtotal = document.getElementById( 'materialtotal' ).value
}
```
##### Share on other sites

Also, when you're operating with values of input field, `parseFloat()` or `parseInt()` the valu and save it in a variable, that way, you don't need to iterate over the same value over and over again.

##### Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.