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
4 odpowiedzi
+ 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(......);
...
});
0
ty but why doesnt it work in my code when the syntax is correct?
0
thanks to both of you.
- 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>