# Javascript: calculating form

**URL:** <https://forum.kirupa.com/t/javascript-calculating-form/252639>\
**Category:** Uncategorized\
**Created:** [February 21, 2008, 10:45pm UTC](https://forum.kirupa.com/t/javascript-calculating-form/252639 "2008-02-21T22:45:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Limitlis](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/limitlis/32/788_2.png) [@Limitlis](https://forum.kirupa.com/u/Limitlis)\
**Post date:** [February 21, 2008, 10:45pm UTC](https://forum.kirupa.com/t/javascript-calculating-form/252639/1 "2008-02-21T22:45:09Z")

</div>

Hi. Im creating a pretty simple form with values that need to be calculated.

I got that portion working… sorta.

Right now it captures the current value in the total field to add the next value to it.

The problem is that even if I go back to a previous cell and make no change it will add to the total again.

Do I have to capture the qty before a change and after to see if a change has been made?

Here’s my code so far:

```auto
<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript" language="javascript">
<!--
function calcVals(price,qty){
    var total = document.getElementById("valTotal");
    var currentTotal = total.value.replace('$','');

    var thisTotal = price * qty;
    
    var newTotal = eval(Number(currentTotal) + Number(thisTotal));
    total.value = newTotal;
}
-->
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="" >
  $0.25<input type="text" name="textfield" title=".25" onblur="calcVals(this.title,this.value);"/><br/>
  $0.12<input type="text" name="textfield1" title=".12" onblur="calcVals(this.title,this.value);"/><br/>
  $5.00<input type="text" name="textfield2" title="5.00" onblur="calcVals(this.title,this.value);"/><br/>
  <a href="#" >Total</a>
  <input type="text" readonly="" name="valTotal" id="valTotal" value="$0"/>
</form>
</body>
</html>

```

Im just wondering what the best method to go about this is.

Just off the top of my head It seems like I would have to capture the qty (value) onFocus then check against the value onBlur then make the necessary calculation.

Any thoughts?

Thanks in advance.
