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
1

1 Answer 1

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));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.