Why Sololearn cant run code with arrow function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why Sololearn cant run code with arrow function?

When i run the code appears error: Connot set propertis of null (settings 'onclick') If I uploading JS from dropbox it works fine https://code.sololearn.com/Wf4GNb8Z9UVk/?ref=app

15th Jan 2023, 11:01 AM
nprNikita
nprNikita - avatar
2 Answers
+ 6
It is not because arrow function it is because script typed in js tab run before html(on android, some people said it run well on ios), and you try to add onclick event to element what is not rendered yet, so it does not exist and give you error. This error mean element does not exist(you mostly get this if script run before html or you mistyped selector name/class/id) Solution is to type js code in script at end of </body> tag like you do with script from dropbox. Or to wrap js code in, this is solution for javascript code here on sololearn: window.onload = () => { // your full script (or just part of script what select elements) } Also using "defer" keyword allow script to run after html is loaded. With this your script(what depend of html loading) can be typed in head tag or at beginning of body tag.
15th Jan 2023, 11:31 AM
PanicS
PanicS - avatar
+ 1
Thank you, forgot about that😅
15th Jan 2023, 11:38 AM
nprNikita
nprNikita - avatar