I have written an increbily simple event listener and yet it comes up with the error: Uncaught TypeError: Cannot call method 'addEventListener' of null
which suggests it is to do with the id maybe (also it works with document
?
<html>
<head>
<script type="text/javascript">
function message () {
alert("Hello!");
}
var button = document.getElementById('button');
button.addEventListener('click', message, true);
</script>
</head>
<body>
<input type="button" id="button" value="Click me!" />
</body>
</html>
(I know I'm going to feel stupid after this, but I am a JS noob)
` , the element you're referencing was not loaded yet.
– Benjamin Gruenbaum Jul 18 '13 at 07:27