simple javascript help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

simple javascript help

i understand "innerHTML" can be used to change the content within an element but why is it not working in the following code? https://code.sololearn.com/W72uqJzSe0yF/#js

3rd Dec 2017, 8:16 PM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
4 Answers
+ 1
Mogammad, the script is executing before the <h1> is even on the page. You'll want to listen for when the page is done loading. window.addEventListener('DOMContentLoaded', () => { ... = document.getElementById(......); ... });
3rd Dec 2017, 9:15 PM
Schindlabua
Schindlabua - avatar
0
ty but why doesnt it work in my code when the syntax is correct?
3rd Dec 2017, 9:10 PM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
0
thanks to both of you.
3rd Dec 2017, 9:28 PM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
- 1
I've put your js code in script in your html file. I gave an id to your h1 And then I changed to getElementById and it works just fine. <h1 id="h1">heading </h1> <script> document.getElementById("h1").innerHTML = "itIsChanged!"; </script>
3rd Dec 2017, 9:03 PM
Elias Papachristos
Elias Papachristos - avatar