+ 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.
11 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"}
+ 5
It's working for me.
+ 4
It's working fine for me,
maybe a little network issue or something like that.
Wait and revisit the website, it should work
+ 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);
});
+ 3
Thanks @ODLNT it works fine when I give window.show .Also thanks everyone .
+ 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.
+ 2
No problem I'm glad I could help. Keep coding.
+ 1
Well, It's showing a reference error in the website of sololearn:
https://imgur.com/tRw9TyJ
+ 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?