24th Jul 2020, 3:11 PM
Ferenando
Ferenando - avatar
4 Respostas
+ 4
The JavaScript error is from how you tried to access the document to link your click event handler before the document's content fully loaded. Here is a fixed version of your JavaScript: document.addEventListener('DOMContentLoaded', function() { var left = document.getElementById('left'); left.onclick = function (){ alert ("test") } });
24th Jul 2020, 3:14 PM
Josh Greig
Josh Greig - avatar
+ 1
Ferenando my answer wasn't best as I didn't provide an explanation unlike Josh Greig
24th Jul 2020, 3:37 PM
Abhay
Abhay - avatar
0
I think it's about lef. Onclick, if you want to make it simple, better delete that before the function, put a name to the function and use it as an onclick attribute within the tag and function name., it's easier like this, or instead you can use the getElement
24th Jul 2020, 3:14 PM
Sam Vásquez
Sam Vásquez - avatar
0
Or try putting code inside following function window.onload=function(){ }
24th Jul 2020, 3:16 PM
Abhay
Abhay - avatar