0
Javascript DOM
When i run it, theelement h1, stays the same. It does not change to hello world... why is it so? <html> <head> <title>Practice JS</title> <script> var elem = document.getElementById("hello"); elem.innerHTML = "Hello World!"; </script> </head> <body> <h1 id="hello">Shameer</h1> </body> </html>
3 Réponses
+ 2
Try to insert your script in the end of the document (before closing tag </body>). You try to change the DOM when is not loaded
+ 1
Yes, it can. But better practice is make external file.js and put link on it in the head section with attribute async or defer. Example:
<script defer src="example.js">
or
<script async src="example.js">
0
So javascript code can be put in the body as well? as long as the code is in a script element? and thank you.