xxxxxxxxxx
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
<script type ="text/javascript">
what();
function what(){
document.getElementById('hello').innerHTML = 'hi';
};
</script>
</head>
<body>
<div id="hello"></div>
</body>
</html>
xxxxxxxxxx
/* Answer to: "Cannot set property 'innerHTML' of null" */
window.onload = function (){
}
/*
Make sure the <script> tag, containing the javascript is at the
bottom of the <body> tag.
As long as the <script> tag is below the element it's trying to
find, the JavaScript should work.
However, if you don't want to be the <script> at the bottom of
your <body> tag. Wrap your javascript with:
*/
xxxxxxxxxx
const element = document.getElementById('myElement');
if (element) {
element.innerText = 'Hello, world!';
} else {
console.error('Element does not exist or is null/undefined.');
}