1. Do you ever call
initialize()
? I did not see that in your code.
2. What does
addEvent do? It threw an error in my testing.
For testing what you did, I made a simple html page and added things in until I got it to work. This was helped by using the
debug mode of the browser by right clicking on the page and choosing
inspect. Just use the
console tab and you should be able to troubleshoot.
I added a script at the end of the page to call
initialize and changed
addEvent to
addEventListener
, with slightly different syntax
After about 5 minutes I got this simple page to work
<html>
<head>
<script type="text/javascript">
function initialize() {
if (document.getElementById) {
var oInputA = document.getElementById('inputA');
var oInputB = document.getElementById('inputB');
var oButton = document.getElementById('add');
var oOutput = document.getElementById('output');
if (oInputA && oInputB && oButton && oOutput) {
oButton.addEventListener('click', addIt);
}
}
}
function addIt() {
var value1 = document.getElementById("inputA").value;
var value2 = document.getElementById("inputB").value;
document.getElementById("output").value = parseFloat(value1) + parseFloat(value2)
}
</script>
</head>
<body>
<input type="text" id="inputA"><br>
<input type="text" id="inputB"><br>
<input type="button" id="add" value="add"><br>
<input type="text" id="output"><br>
<script>initialize();</script>
</body>
</html>
Reference:
EventTarget.addEventListener() - Web APIs | MDN[
^]