0

I have two inputs from textboxes.and i want to display sum of those two values in another textbox.this is how i did it.but when i entered 2and 3 it display 23 in 3rd text box.i want to display 5 on 3rd text box.what is the wrong and how can solve it?

    var sale=document.getElementById("sale").value;  // enter 2

    var sale2=document.getElementById("sale2").value; // enter 3


    document.getElementById('calsale').value = ((sale2)+(sale)); //now display23.want  to display 5
Tje
  • 71
  • 1
  • 3
  • 15
  • ... see also http://stackoverflow.com/questions/11961474/how-to-sum-two-numbers-from-input-tag – Matt Aug 28 '14 at 08:18

1 Answers1

3

You should use parseInt function. In facts, values get tanks to getElementById("sale").value are string and not numbers. You have to cast them into real integers to make the sum (otherwise it will make the concatenation of the two strings)

var sale=parseInt(document.getElementById("sale").value);  // enter 2
var sale2=parseInt(document.getElementById("sale2").value); // enter 3

document.getElementById('calsale').value = ((sale2)+(sale));
Gwenc37
  • 2,064
  • 7
  • 18
  • 22