Hi,
The following code generate the error “Type error: Cannot read property style of null”
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Query select</title>
<script src="domtest.js"></script>
</head>
<body>
<h1>My name is Sammy</h1>
</body>
</html>
var h= document.querySelector("h1");
var isGreen=false;
setInterval(function(){
if(isGreen){
h.style.color="black";
} else {
h.style.color="green";
}
isGreen=!isGreen
},1000)
I know where is the problem its in this line:
“var h= document.querySelector(“h1”);”
because I paste it in the console and returned h=null
but I don’t know why this happened,