This content originally appeared on CodeSource.io and was authored 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
The post Solved – cannot set property ‘innerhtml’ of null appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Deven
Deven | Sciencx (2021-03-11T06:14:59+00:00) Solved – cannot set property ‘innerhtml’ of null. Retrieved from https://www.scien.cx/2021/03/11/solved-cannot-set-property-innerhtml-of-null/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.