Solved – cannot set property ‘innerhtml’ of null

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…

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

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Solved – cannot set property ‘innerhtml’ of null." Deven | Sciencx - Thursday March 11, 2021, https://www.scien.cx/2021/03/11/solved-cannot-set-property-innerhtml-of-null/
HARVARD
Deven | Sciencx Thursday March 11, 2021 » Solved – cannot set property ‘innerhtml’ of null., viewed ,<https://www.scien.cx/2021/03/11/solved-cannot-set-property-innerhtml-of-null/>
VANCOUVER
Deven | Sciencx - » Solved – cannot set property ‘innerhtml’ of null. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/11/solved-cannot-set-property-innerhtml-of-null/
CHICAGO
" » Solved – cannot set property ‘innerhtml’ of null." Deven | Sciencx - Accessed . https://www.scien.cx/2021/03/11/solved-cannot-set-property-innerhtml-of-null/
IEEE
" » Solved – cannot set property ‘innerhtml’ of null." Deven | Sciencx [Online]. Available: https://www.scien.cx/2021/03/11/solved-cannot-set-property-innerhtml-of-null/. [Accessed: ]
rf:citation
» Solved – cannot set property ‘innerhtml’ of null | Deven | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.