+ 6
"add" is not Defined - Javascript [SOLVED]
How do I get around this error? I tried using addEventListener() but the method called itself before pressing the button. Why won't the method call itself without error? https://code.sololearn.com/W34ytr86PioV/?ref=app
4 Answers
+ 3
~ swim ~ andri haryo saputro Thanks, works now, but when I try to interact with things from the matter.js library it cannot access them. And the actual canvas doesn't load without window.onload() so now I am stuck with another problem. Have you got any ideas?
+ 3
Thanks for your patience and your answer
+ 1
Put the function add() outside the window.onload.
The function add() cant be recognize because its only run when the page is loaded first. So your event onclick doesnt work and cant find the add() .
beside if you want to call a function with window.onload you can do it like this :
window.onload = function () { add() };
function add() { alert('hello') }
so function add can be running when the page loaded and with the event like onclick, onchange, etc.