Here is an example where it automatically calculates when you type numbers into the fields.
It uses oninput
to detect when the up/down arrows of the input field are pressed, and it uses onkeyup
to detect when the user types numbers.
It will clear the result if either field is empty.
function sum() {
var a = document.getElementById('a').value;
var b = document.getElementById('b').value;
var result = document.getElementById('result');
if (!a || !b){
result.innerHTML = '';
} else {
result.innerHTML = parseInt(a) + parseInt(b);
}
}
<input id="a" type="number" oninput="sum()" onkeyup="sum()" /> +
<input id="b" type="number" oninput="sum()" onkeyup="sum()" /> =
<span id="result"></span>