Why innerHTML is not working?

Help why innerHTML is not working?

JS:
document.getElementById('message').innerHTML = 'Hello';


HTML:
<!DOCTYPE html>
<html>

the <head>
<title>Js</title>
 <meta charset="utf-8">
 <script src="js/java_script.js"> </script>
</head>

the <body>
 <div id="message"></div>
</body>

</html>
March 19th 20 at 09:18
3 answers
March 19th 20 at 09:20
Solution
Because the script is connected to head and the DOM is not yet formed at the time of the search item.
March 19th 20 at 09:22
Solution
Or write:
the <head>
<title>Js</title>
 <meta charset="utf-8">
</head>

the <body>
 <div id="message"></div>
 <script src="js/java_script.js"> </script>
</body>

</html>


either
in JS
document.addEventListener('DOMContentLoaded', function() {
 document.getElementById('message').innerHTML = 'Hello';
}, false);
March 19th 20 at 09:24
Because it is necessary to store in a variable you received early a DOM element

var myElement = document.getElementById('message').innerHTML = 'Hello';

Find more questions by tags JavaScript