I am using WebStorm IDE and I'm writing a simple test for my HTML and JS. Here's the code:
JavaScript (index.js):
const cool = document.getElementByClassName('error');
cool.innerText = 'test';
HTML (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src = "index.js"></script>
</head>
<body>
<h1 class="theWhat">The DOM</h1>
<p>Hello World</p>
<p class="error">Error!</p>
</body>
</html>
The JavaScript does nothing to any of the 'p' tags and when I run it in it of itself, I et this error:
const pars = document.querySelectorAll('p'); ^ ReferenceError: document is not defined at Object. (C:\Users\sethz\WebstormProjects\jscourse\chapter2\index.js:124:14)
←[90m at Module._compile (node:internal/modules/cjs/loader:1101:14)←[39m
←[90m at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)←[39m
←[90m at Module.load (node:internal/modules/cjs/loader:981:32)←[39m
←[90m at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m
←[90m at Function.executeUserEntryPoint as runMain←[39m
←[90m at node:internal/main/run_main_module:17:47←[39m
Process finished with exit code 1
Ive read it has to do with the interpreter, but I have no idea how to fix that.
NOTE:
I'm using Node.js. I've nevr had this problem before until I deleted it in order to update to the most recent WebStorm (2021)
parsis a NodeList (an Array like object) - so, trying to use it as if it were an Element won't workcoolis a NodeList ... the name of the variable doesn't matter, the content is the problem -anyway, that isn't your issue, your issue is you're apparently trying to execute a web page using nodejs - I think you need to step back and realise that a webpage is displayed on a browser - and that your "code" is HTML (with a script in it), not javascript (<script src = index.js></script>is missing qouts, it should become:<script src="index.js"></script>add to that your javascript is being executed before the dom is loaded, and when usingdocument.querySelectorAll()it'll return aNodeListwhich you need to loop through its elements to change them one by one:const pars = document.querySelectorAll('p'); pars.forEach(el => el.innerHTML = "test");