Why the code is not working? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Why the code is not working?

I am learning javascript on the sololearn website. Now I am learning the handling events lesson. But some code given in try it yourself's code is giving errors. Can anyone please tell me why? Here is the image: https://imgur.com/tRw9TyJ and here is the code: https://code.sololearn.com/WA2a1a117A6A/# Thanks in advance.

13th May 2021, 6:06 AM
The future is now thanks to science
The future is now thanks to science - avatar
9 Answers
+ 7
The problem is that sololearn website wraps javascript in an iife when it is written the javascript tab [using chrome, firefox, and edge] https://imgur.com/Dkm3n6k (function () { function show() { alert("Hi there"); } })(); instead of function show() write function as a property of window, window.show = function(){alert "Hi there"}
13th May 2021, 6:39 AM
ODLNT
ODLNT - avatar
+ 5
It's working for me.
13th May 2021, 6:08 AM
Rohit
+ 4
It's working fine for me, maybe a little network issue or something like that. Wait and revisit the website, it should work
13th May 2021, 6:20 AM
ACADEMIC
ACADEMIC - avatar
+ 3
https://stackoverflow.com/questions/17378199/uncaught-referenceerror-function-is-not-defined-with-onclick https://stackoverflow.com/questions/17583544/referenceerror-variable-is-not-defined [HTML] <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <button id="btn">Click Me</button> </body> </html> [JS] function show() { alert("Hi there"); } window.addEventListener("load", (event) => { document.getElementById("btn").addEventListener("click", show); });
13th May 2021, 6:38 AM
Rohit
+ 3
Thanks @ODLNT it works fine when I give window.show .Also thanks everyone .
13th May 2021, 7:04 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 2
The future is now thanks to science got where is the problem, check if your file directory is correct or not if you don't well connect your html file to js then there is no defined show function which gives a refrence error.
13th May 2021, 6:39 AM
ACADEMIC
ACADEMIC - avatar
+ 2
No problem I'm glad I could help. Keep coding.
13th May 2021, 7:07 AM
ODLNT
ODLNT - avatar
+ 1
Well, It's showing a reference error in the website of sololearn: https://imgur.com/tRw9TyJ
13th May 2021, 6:13 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
@ACADEMIC I don't think it's a network issue because I tried the other code and they're working fine and also if I write the same code in the script tag of HTML it's working fine. Example: https://code.sololearn.com/W9a4A22a24a1 This code is working fine on the website but the above code mentioned in the question doesn't.Is it a bug or something else?
13th May 2021, 6:28 AM
The future is now thanks to science
The future is now thanks to science - avatar