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>

16th Aug 2017, 3:12 PM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
3 odpowiedzi
+ 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
16th Aug 2017, 3:34 PM
devilCoder
devilCoder - avatar
+ 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">
16th Aug 2017, 3:48 PM
devilCoder
devilCoder - avatar
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.
16th Aug 2017, 3:39 PM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar