Thursday, February 14, 2019

Cannot set property 'innerHTML'

In Javascript, we can place the code in the <head> tag , <body> tag, and place in external file. 
So  if the error like this  Uncaught TypeError: Cannot set property 'innerHTML' of null " 
<!DOCTYPE html>
<html>
<head>
<title>Use JS in head </title>
<script >
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
</head>
<body>
<h1>Use Js in Head tag</h1>
<p id="demo">A Paragraph</p>
</body>

</html>

Change to 

<!DOCTYPE html>
<html>
<head>
<title>Use JS in head </title>

</head>

<body>
<h1>Use Js in Head tag</h1>
<p id="demo">A Paragraph</p>
</body>
<script >
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
</html>

You have to place the hello div or id before the script