i am trying to get the style of an element from html and trying to print on console. I cant see style option while typing the style property in visual code. its keep showing the error Uncaught TypeError: Cannot read properties of null (reading 'style') in javascript var element; element = document.querySelector("#header").style.backgroudColor; console.log(element);
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Page Title</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='all.css'>
<script src="javascript_code.js"></script>
</head>
<body>
<div id="header" style="background-color: red;">
<h1>this is header</h1>
<h1>this is header2</h1>
</div>
<script src="javascript_code.js"></script>
</body>
</html>
CodePudding user response:
you have included javascript file two time first in the header and second at the last inside body tag. first remove one of them and there is a spelling mistake in backgroundColor as you can see here element = document.querySelector("#header").style.backgroundColor = 'green'
;
CodePudding user response:
you can use document.getElementById('header').style.backgroudColor
like this.
CodePudding user response:
i restarted the computer and all my snippets started working. the style property is now working.