0

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)

10
  • 2
    apart from that error - the code is wrong anyway .. .since pars is a NodeList (an Array like object) - so, trying to use it as if it were an Element won't work Commented Aug 10, 2021 at 17:22
  • Are you running this javascript code on node js? Commented Aug 10, 2021 at 17:24
  • 2
    @ssethzz document object is works on browser javascript. Node js run time environment is different from browser javascript. Commented Aug 10, 2021 at 17:35
  • 1
    @ssethzz - wha? well, I guess now cool is 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 ( Commented Aug 10, 2021 at 17:37
  • 1
    You have multiple issues, First: <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 using document.querySelectorAll() it'll return a NodeList which you need to loop through its elements to change them one by one: const pars = document.querySelectorAll('p'); pars.forEach(el => el.innerHTML = "test"); Commented Aug 10, 2021 at 18:10

2 Answers 2

0

To include a javascript file to a html page you need to declare static directory in your index.js/ app.js.

const express = require('express');
const app = express();
app.use(express.static('public')) // static file
app.listen(8000, console.log(`Server started on port 8000`))

We declared 'public' folder in root as static file folder. Then if you have a public/index.js (For browser) in public folder, add it to your html page as usual

<script src="/index.js"></script>

Note that, you don't need to specify the public folder when using the static file in browser.

Sign up to request clarification or add additional context in comments.

Comments

0

I appreciate all the help guys. I've used some answers and some worked. I have found another way to do this without the extra code as well. I went to the website http://vanilla-js.com/ which downloads a script file that includes DOM manipulation. All I have to do is refractor it and change its name to my liking in my IDE (WebStorm).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.