Javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Javascript

I want to get alert when clicked on button , so anyone please help. https://code.sololearn.com/WSK8i3mRap4c/?ref=app

17th Oct 2023, 4:54 AM
GHOST
GHOST - avatar
6 Answers
+ 10
Remember to wrap JavaScript code in Sololearn's compiler in the window.onload
17th Oct 2023, 7:34 AM
Ausgrindtube
Ausgrindtube - avatar
+ 5
GHOST ,, As Ausgrindtube bro said,continue.. You can use script tag... See this modified version of your code example.. https://code.sololearn.com/WxISCmJ8Hymb/?ref=app
17th Oct 2023, 8:34 AM
Darpan kesharwani🇮🇳[Inactive📚]
Darpan kesharwani🇮🇳[Inactive📚] - avatar
+ 4
As Ausgrindtube mentioned. In the playground you have to wrap your function into the window.onload. This ensures the code is executed only after the entire page is loaded. Here's an example. window.onload = function() { const x = "HELLO"; document.getElementById("btn").onclick = function (){ alert(x); } }
19th Oct 2023, 2:29 AM
Chris Coder
Chris Coder - avatar
+ 2
Looks like it already works
17th Oct 2023, 5:57 AM
Raul Ramirez
Raul Ramirez - avatar
+ 2
Thanks ausgrindtube
17th Oct 2023, 8:58 AM
GHOST
GHOST - avatar
0
You didn’t specify the btn id
18th Oct 2023, 9:58 PM
Annihilate
Annihilate - avatar