"add" is not Defined - Javascript [SOLVED] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

6th May 2019, 1:56 PM
Clueless Coder
Clueless Coder - avatar
3 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?
6th May 2019, 2:09 PM
Clueless Coder
Clueless Coder - avatar
+ 3
Thanks for your patience and your answer
6th May 2019, 2:25 PM
Clueless Coder
Clueless Coder - avatar
+ 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.
6th May 2019, 2:04 PM
Andri Hry
Andri Hry - avatar