So I am new to javascript and I am practicing event handlers. I created the event handler when the button is pressed but nothing happens. I tried to look at calculators online but they are all using jquery or inline js! I want to avoid those since I am trying to get better at event handlers. Here is the code I am working on the button 0, with id "n0:
<!DOCTYPE html>
<html>
<head>
<title>
My Javascript Calculator
</title>
</head>
<body>
<div id="calculator">
<input type="text" name="display" disabled>
<br>
<div id="keypad">
<button id="clrEntry">CE</button>
<button id="clear">C</button>
<button id="divide">/</button>
<button id="multiply">*</button>
<br>
<button id="n1">1</button>
<button id="n2">2</button>
<button id="n3">3</button>
<button id="add">+</button>
<br>
<button id="n4">4</button></button>
<button id="n5">5</button>
<button id="n6">6</button>
<button id="subtract">-</button>
<br>
<button id="n7">7</button>
<button id="n8">8</button>
<button id="n9">9</button>
<button id="equal">=</button>
<br>
<button id="n0">0</button>
</div>
</div>
<script src="calculator.js"></script>
</body>
</html>
Here are the contents of calculator.js:
function init()
var memory;
{
document.getElementById('n0').addEventListner("click", number0);
}
function number0()
{
document.getElementById('display').value += 0;
memory += 0;
return 0;
}
window.addEventListner("load", init, false);
<script type="text/javascript">wrapped around it if it will be in your html file or be in a separate .js fileaddEventListnertoaddEventListener. Besides that, it looks like your variablememoryshould be declared before you dofunction init(). A JavaScript console can tell you where the errors are; try using one, every modern browser has one. Good luck :)