Why do I get an error or Uncaught TypeError: Cannot set property 'innerHTML' of null? I thought I understood innerHTML and had it working before.
<!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>
This question is related to
javascript
onload
innerhtml
This error can appear even if you load your script after the html render is finished. In this given example your code
<div id="hello"></div>
has no value inside the div. So the error is raised, because there is no value to change inside. It should have been
<div id="hello">Some random text to change</div>
then.
Add jquery into < head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
Use $document.ready() : the code can be in < head>
or in a separate file like main.js
1) using js in same file (add this in the < head>
):
<script>
$( document ).ready(function() {
function what(){
document.getElementById('hello').innerHTML = 'hi';
};
});
</script>
2) using some other file like main.js (add this in the < head>
):
<script type="text/javascript" src="/path/to/main.js" charset="utf-8"></script>
and add the code in main.js file :)
Is this running in the <head>
of the html? If so, you need to make sure the <body>
tag has actually loaded first.
You need to use an onload handler, for example: http://javascript.about.com/library/blonload.htm
Just put your JS in window.onload
window.onload = function() {
what();
function what() {
document.getElementById('hello').innerHTML = 'hi';
};
}
Let us first try to understand the root cause as to why it is happening in first place.
Why do I get an error or Uncaught TypeError: Cannot set property 'innerHTML' of null?
The browser always loads the entire HTML DOM from top to bottom. Any JavaScript code written inside the script
tags (present in head
section of your HTML file) gets executed by the browser rendering engine even before your whole DOM (various HTML element tags present within body tag) is loaded. The scripts present in head
tag are trying to access an element having id hello
even before it has actually been rendered in the DOM. So obviously, JavaScript failed to see the element and hence you end up seeing the null reference error.
How can you make it work as before?
You want to show the "hi" message on the page as soon as the user lands on your page for the first time. So you need to hook up your code at a point when you are completely sure of the fact that DOM is fully loaded and the hello
id element is accessible/available. It is achievable in two ways:
hello
id element is already loaded. You can achieve it by simply moving the script tag after all the DOM elements i.e. at the bottom where body
tag is ending. Since rendering happens from top to bottom so your scripts get executed in the end and you face no error. <!DOCTYPE HTML>_x000D_
<html>_x000D_
<head>_x000D_
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">_x000D_
<title>Untitled Document</title>_x000D_
</head>_x000D_
_x000D_
<body>_x000D_
<div id="hello"></div>_x000D_
_x000D_
<script type ="text/javascript">_x000D_
what();_x000D_
function what(){_x000D_
document.getElementById('hello').innerHTML = 'hi';_x000D_
};_x000D_
</script>_x000D_
</body>_x000D_
</html>
_x000D_
window.onload
event which gives you the hint that browser has finished loading the DOM. So by the time when this event gets fired, you can be rest assured that your element with hello
id already loaded in the DOM and any JavaScript fired thereafter which tries to access this element will not fail. So you do something like below code snippet. Please note that in this case, your script works even though it is present at the top of your HTML document inside the head
tag. <!DOCTYPE HTML>_x000D_
<html>_x000D_
<head>_x000D_
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">_x000D_
<title>Untitled Document</title>_x000D_
<script type ="text/javascript">_x000D_
window.onload = function() {_x000D_
what();_x000D_
function what(){_x000D_
document.getElementById('hello').innerHTML = 'hi';_x000D_
};_x000D_
}_x000D_
</script>_x000D_
</head>_x000D_
_x000D_
<body>_x000D_
<div id="hello"></div>_x000D_
</body>_x000D_
</html>
_x000D_
You could try using the setTimeout method to make sure your html loads first.
Javascript looks good. Try to run it after the the div has loaded. Try to run only when the document is ready. $(document).ready
in jquery.
The error is self-explaining it is not getting the HTML tag in which You want to set the Data So make tag available to JS then only You can set Data to that.
I have had the same problem and it turns out that the null error was because I had not saved the html I was working with.
If the element referred to has not been saved once the page is loaded is 'null', because the document does not contain it at the time of load. Using window.onload also helps debugging.
I hope this was useful to you.
You need to change div
into p
. Technically innerHTML means it is inside the <??? id=""></???>
part.
Change:
<div id="hello"></div>
into
<p id="hello"></p>
Doing:
document.getElementById('hello').innerHTML = 'hi';
will turn
<div id="hello"></div> into this <div id="hello">hi</div>
which actually does not make sense.
You can also try to change:
document.getElementById('hello').innerHTML = 'hi';
into this
document.getElementById('hello').innerHTML='<p> hi </p> ';
to make it work.
There are different possible cause as discussed would just like to add this for someone who might have the same issue as mine.
In my case I had a missing close div as shown below
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
</head>
<body>
<div> //I am an open div
<div id="hello"></div>
<script type ="text/javascript">
what();
function what(){
document.getElementById('hello').innerHTML = 'hi';
};
</script>
</body>
</html>
Missing a close div can result in disorganization of the transversal from child to parent or parent to child hence resulting in an error when you try to access an element in the DOM
You could tell javascript to perform the action "onload"... Try with this:
<script type ="text/javascript">
window.onload = function what(){
document.getElementById('hello').innerHTML = 'hi';
};
</script>
No doubt, most of the answers here are correct, but you can also do this:
document.addEventListener('DOMContentLoaded', function what() {
document.getElementById('hello').innerHTML = 'hi';
});
Here Is my snippet try it. I hope it will helpfull for u.
<!DOCTYPE HTML>_x000D_
<html>_x000D_
<head>_x000D_
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">_x000D_
<title>Untitled Document</title>_x000D_
</head>_x000D_
_x000D_
<body>_x000D_
_x000D_
<div id="hello"></div>_x000D_
_x000D_
<script type ="text/javascript">_x000D_
what();_x000D_
function what(){_x000D_
document.getElementById('hello').innerHTML = 'hi';_x000D_
};_x000D_
</script>_x000D_
</body>_x000D_
</html>
_x000D_
You are almost certainly running your code before the DOM is constructed. Try running your code in a window.onload
handler function (but see note below):
window.onload = function() {
// all of your code goes in here
// it runs after the DOM is built
}
Another popular cross-browser solution is to put your <script>
block just before the closing </body>
tag. This could be the best solution for you, depending on your needs:
<html>
<body>
<!-- all of your HTML goes here... -->
<script>
// code in this final script element can use all of the DOM
</script>
</body>
</html>
window.onload
will wait until all images and subframes have loaded, which might be a long time after the DOM is built. You could also use document.addEventListener("DOMContentLoaded", function(){...})
to avoid this problem, but this is not supported cross-browser. The bottom-of-body trick is both cross-browser and runs as soon as the DOM is complete.maybe this will work:
document.getElementsByTagName("body")[0].innerHTML
<!DOCTYPE HTML>_x000D_
<html>_x000D_
<head>_x000D_
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">_x000D_
<title>Untitled Document</title>_x000D_
</head>_x000D_
<body>_x000D_
<div id="hello"></div>_x000D_
<script type ="text/javascript">_x000D_
what();_x000D_
function what(){_x000D_
document.getElementById('hello').innerHTML = 'hi';_x000D_
};_x000D_
</script>_x000D_
</body>_x000D_
</html>
_x000D_
The root cause is: HTML on a page have to loaded before javascript code. Resolving in 2 ways:
1) Allow HTML load before the js code.
<script type ="text/javascript">
window.onload = function what(){
document.getElementById('hello').innerHTML = 'hi';
}
</script>
//or set time out like this:
<script type ="text/javascript">
setTimeout(function(){
what();
function what(){
document.getElementById('hello').innerHTML = 'hi';
};
}, 50);
//NOTE: 50 is milisecond.
</script>
2) Move js code under HTML code
<div id="hello"></div>
<script type ="text/javascript">
what();
function what(){
document.getElementById('hello').innerHTML = 'hi';
};
</script>
Let the DOM load. To do something in the DOM you have to Load it first. In your case You have to load the <div>
tag first. then you have something to modify. if you load the js first then that function is looking your HTML
to do what you asked to do, but when that time your HTML
is loading and your function cant find the HTML
. So you can put the script in the bottom of the page. inside <body>
tag then the function can access the <div>
Because DOM is already loaded the time you hit the script.
The JavaScript part needs to run once the page is loaded, therefore it is advised to place JavaScript script at the end of the body tag.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Example</title>
</head>
<body>
<div id="hello"></div>
<script type ="text/javascript">
what();
function what(){
document.getElementById('hello').innerHTML = 'hi';
};
</script>
</body>
</html>
_x000D_
I have done all the solutions mentionned here. but they didn't work until i have made this one using Jquery :
in my HTML page :
> <div id="labelid" > </div>
and when i click on a button, i put this in my JS file :
$("#labelid").html("<label>Salam Alaykom</label>");
<!DOCTYPE HTML>_x000D_
<html>_x000D_
<head>_x000D_
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">_x000D_
<title>Example</title>_x000D_
_x000D_
</head>_x000D_
<body>_x000D_
<div id="hello"></div>_x000D_
<script type ="text/javascript">_x000D_
what();_x000D_
function what(){_x000D_
document.getElementById('hello').innerHTML = '<p>hi</p>';_x000D_
};_x000D_
</script> _x000D_
</body>_x000D_
</html>
_x000D_
Source: Stackoverflow.com