Need on Sololearn's Code Playground | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need on Sololearn's Code Playground

Why is my script (code below) giving me an error on the JS block but it's working if I put it on HTML <script></script>? https://code.sololearn.com/W1RtNXI85H7a/?ref=app

12th May 2020, 3:15 PM
Sidepocket
Sidepocket - avatar
2 Answers
+ 2
Answer is quite simple, Sololearn by default links our script to head tag, so when you write your js in JS tab then you are selecting element before it is even created. You have to wait for it to load using onload event of window. window.onload = function() { document.getElementsByTagName("li")[2].style.color= "red"; }
12th May 2020, 3:23 PM
Raj Chhatrala
Raj Chhatrala - avatar
0
🔫 Rick it's working fine now, thanks brother.
12th May 2020, 3:25 PM
Sidepocket
Sidepocket - avatar