+ 6

Does Javascript document.getElementById function in SoloLearn editor?

Please answer. I've a snippet of code with document.getElementById functioning in browser, but it seems it returns null in SoloLearn editor: var elem = document.getElementById('para'); elem.style.color = newColor; and in HTML body: <p id="para">a paragraph</p>

13th Jan 2019, 9:29 PM
Paolo De Nictolis
Paolo De Nictolis - avatar
2 Antworten
+ 3
The problem is that the js file is probably automatically loaded in the header or beginning of the body document by SoloLearn. What you need to do is window.onload = () => { elem = document.getElementById(`para`) elem.style.color = newColor }
25th Jan 2019, 2:46 PM
Martin Möhle
Martin Möhle - avatar
+ 2
Paolo You could check someone else code to find the answer to your question https://code.sololearn.com/WuSoJtZrSlrU/#js The more likely problem is your code itself, you should provide a link to allow a community member to help you get to the root of your problem. btw- Check newColor
13th Jan 2019, 10:33 PM
ODLNT
ODLNT - avatar