I try to implement the following simple equation (5+x=?)
using html and JavaScript:
function count() {
var x = document.getElementById("myInput").value;
document.getElementById('execute').innerHTML = x + 5;
}
<div> 5 + <input type="number" id="myInput">
<input type="button" value="=" onclick="count()">
<span id="execute">?</span>
</div>
Unfortunately when I write for example 2 in the input field and then click on the button (=)
it gives me as answer the number 52, instead of the answer that I would like to see – 7.
It seems that it just joins the new number next to 5, instead of adding it, like it is a string and not a number. I know it’s not the brightest function and it’s not very useful, but I will appreciate if you could give me a hint where I am wrong.
This will help me understand better JavaScript and continue my education.