Why did I write this addition didn't work?

Jul.06,2021

buy and sell. Fnum and snum, take a good value before the program runs, but not onclick, which is equivalent to a constant, so it is (initial value) 0.


value takes the current value when clicked

var eleFirstNumber = document.getElementById("first-number");
var eleSecondNumber = document.getElementById("second-number");
document.getElementById("add-btn").onclick = function(){
  var fnum = Number(eleFirstNumber.value);
  var snum = Number(eleSecondNumber.value);
  document.getElementById("result").innerHTML =  fnum + snum;
}

should get the input value in onclick, that is,

document.getElementById("add-btn").onclick = function(){
  var fnum = Number(document.getElementById("first-number").value);
  var snum = Number(document.getElementById("second-number").value);
  document.getElementById("result").innerHTML =  fnum + snum;
}

fnum and snum in your code are always the initial values of input 0


get the value first, but not when you click on the event


document.getElementById("add-btn").onclick = function(){
  var fnum = Number(document.getElementById("first-number").value);
  var snum = Number(document.getElementById("second-number").value);
  document.getElementById("result").innerHTML =  fnum + snum;
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e5ae1c-4455f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e5ae1c-4455f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?