+ 1
Uncaught TypeError: Cannot set property 'innerHTML' of null
html code: <html> <head> <meta charset="UTF-8"> <title>DOM</title> </head> <body> <script src="dom.js"></script> <p id="dom2">This is dom</p> </body> </html> JAVASCRIPT: var code=document.getElementById("dom2"); code.innerHTML= "this is DOM"; console.log(code.innerHTML);
7 Réponses
+ 3
This is totally fine, i dont know why u r getting the error. Try putting the javascript inside <script> tags before the ending body tag. Use <scrip> without any src attribute
If it works then the problem is sololearn's it loads script after DOM. You can also use window.onload = function() {
// your js code here
}
+ 5
This is bcz the script loads first even before the DOM loads that's why Pankaj Kumar can't set property of null (means that is not present in dom)
So simple solution is to load dom first then script
This can be done in these ways
Use onload function and put all your script inside that
like this
onload = () =>{
....
}
Or
Place the script just above the body tag
Or
In JS section u can use this way
//</script><script>
+ 3
https://www.sololearn.com/post/90825/?ref=app
Please don't forget to use the search bar 🔍 feature next time to prevent duplicates as recommended in the community guidelines. These problems are very common and have been asked before.
The search bar:
https://www.sololearn.com/post/10362/?ref=app
Similar threads:
https://www.sololearn.com/discuss/2293991/?ref=app
https://www.sololearn.com/discuss/1229047/?ref=app
https://www.sololearn.com/discuss/276929/?ref=app
https://www.sololearn.com/discuss/2387884/?ref=app
https://www.sololearn.com/discuss/2270730/?ref=app
https://www.sololearn.com/discuss/2225100/?ref=app
+ 2
Pankaj Kumar just do the following and you will see it in the web view and on the console view
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DOM</title>
<script>
var code=document.getElementById("dom2");
code.innerText= "this is DOM";
console.log(code.innerText);</script>
</head>
<body>
<p id="dom2"></p>
</body>
</html>
+ 1
Put your js code just before ending body.this error occur because your js is loaded earlier than html hence js is not getting your element in variable.
<body>
//Code
<script>
//js
</script>
</body>
+ 1
Javascript code's ouside the script tags
0
In JavaScript almost everything is an object, null and undefined are exception. if a variable has been declared, but has not been assigned a value, is automatically assigned the value undefined . Therefore, if you try to access the value of such variable, it will throw Uncaught TypeError cannot set property '0' of undefined/null .
JavaScript null and undefined is one of the main reasons to produce a runtime errors . This happens because you don't check the value of unknown return variables before using it. If you are not sure a variable that will always have some value, the best practice is to check the value of variables for null or undefined before using them. The standard way to catch null and undefined simultaneously is this:
if (variable == null) {
// your code here.
}
Because null == undefined is true, the above code will catch both null and undefined.
Also you can write equivalent to more explicit but less concise:
if (variable === undefined variable === null) {
// your code here.
}
This should work for any variable that is either undeclared or declared and explicitly set to null or undefined.
http://net-informations.com/js/err/set.htm