DOM manipulation is not working when JS code is in JS part of Solo Learn editor. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

DOM manipulation is not working when JS code is in JS part of Solo Learn editor.

When I run the following code to he DOM manipulation doesn't work when the JS code is written in the JS part of Solo Learn editor. But it was working on my computer. When I pasted the code inside the script tag in HTML it is working now. Why doesn't it work when it is written in the JS part of the editor? Is there any problem with the Solo Learn editor or am I missing something? https://code.sololearn.com/Ws38VqsaWZ23/?ref=app.

12th Jan 2023, 2:09 AM
Rahul Kumar
Rahul Kumar - avatar
2 Answers
+ 2
This problem is related to the order how the code on the JS tab is loaded. It even works differently on different devices. If the script is executed before the main page is loaded and the DOM is incomplete, then it won't work. Easy fix: on the JS tab enclose all your code in an anonymous function that is bind to the onload event. window.onload = () => { // All your Javascript }
12th Jan 2023, 3:38 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Tibor Santa This is a recurring question. I wonder why the behavior was not implemented by default?
12th Jan 2023, 9:54 AM
Bob_Li
Bob_Li - avatar