Solved – cannot set property ‘innerhtml’ of null
Posted on: March 11, 2021 by Deven
In this article, you will learn how to solve cannot set property ‘innerhtml’ of null error in JavaScript.
Let’s look at a code example that produces the same error.
…
<head>
<title> Hello JavaScript</title>
</head>
<script>
var show_date = document.getElementById("show_date");
var current_date = new Date();
var date = `${current_date.getHours()}:${current_date.getMinutes()}`
show_date.innerHTML = date;
</script>
…
<body>
<p>Current time is <span id="show_date"></span>.</p>
</body>
Output
"<a class='gotoLine' href='#44:22'>44:22</a> Uncaught TypeError: Cannot set property 'innerHTML' of null"
In order to solve cannot set property ‘innerhtml’ of null error in JavaScript by moving our <script>
below our paragraph like in the code snippet below:
<head>
<title> Hello JavaScript</title>
</head>
<body>
<p>Current time is <span id="show_date"></span>.</p>
</body>
<script>
var show_date = document.getElementById("show_date");
var current_date = new Date();
var date = `${current_date.getHours()}:${current_date.getMinutes()}`
show_date.innerHTML = date;
</script>
output
Current time is 11:42
Share on social media
//