Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ADD form values using javascript?

this is the code i came up with but all it does is this 1+1=11 i need it to do 1+1=2.

<head>
<script type="text/javascript">
function startCalc(){
  interval = setInterval("calc()",1);
}
function calc(){
  one = document.form1.quantity.value;
  two = document.form1.price.value;
  c = one + two 
  document.form1.total.value = (c);
}
function stopCalc(){
  clearInterval(interval);
}
</script>


</head>
<body>
<form name="form1">
Quantity: <input name="quantity" id="quantity" size="10">Price: <input name="price" id="price" size="10"><br>
Total: <input name="total" size="10" readonly=true><br>
<input onclick="startCalc();" onmouseout="stopCalc()" type="button" value="Submit">
</form>

</body>

of course this is a really simple form, but you get the idea please help me tell what i'm doing wrong here

like image 630
Atom Vayalinkal Avatar asked Dec 17 '22 17:12

Atom Vayalinkal


1 Answers

You need to use parseInt() to convert the string to an integer.

c = parseInt(one, 10) + parseInt(two, 10)
like image 72
Paul Schreiber Avatar answered Jan 12 '23 16:01

Paul Schreiber