i have linked my script.js file to my index.html file and both files are in same directory. i have linked that script.js file in section but that is not working when i open-up the console it is shwoing
script.js:2 Uncaught TypeError: Cannot read property 'getElementsByTagName' of null
but whenever i link that file in the last part of the section perfectly, even putting that file in the first section of also not working.
inde.html:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="script.js"></script>
<meta charset="UTF-8">
<title>Sample</title>
</head>
<body>
<p id="name">
<span>john</span>
</p>
</body>
</html>
script.js:
var result = document.getElementById("name");
var final = result.getElementsByTagName("span");
console.log(final);
`. either put your code in `window.onload` or move it to the footer.
– rksh1997 Apr 21 '20 at 17:41