-2

I am trying to build a website including HTML, CSS and JS.

I have a HTML file and a JS file. I am trying to give the values of the HTML file to the JS file by using document.getElementById(); but it wont work.

This problem kinda holds me back and I am stuck because of that.

var b = document.getElementById("radioYes"); // a radio button called "yes"
var d = document.getElementById("radioNo"); // a radio button called "no"
var c = document.getElementById("submit1"); // submit-button

What can I do?

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
SlimE
  • 45
  • 1
  • 1
  • 5

2 Answers2

3

Where are you running your js file?

The document object is only available in the browser, and not in the node environment. Try opening your html file in the browser - make sure you add your js script to the html file.

Have a look at this answer: Using Document object in nodejs

Vgoose
  • 249
  • 1
  • 10
2

JavaScript doesn't have a default document global. Browsers provide one, but your Node.js code doesn't run in a browser, it runs in the Node.js environment (e.g., as an application on your workstation, or as a server process, etc.).

To run the code you've shown, you'd include your .js file in a page by using a script tag in the HTML, typically at the end of body just before the closing </body> tag:

<script src="filename.js"></script>
</body>

Then you'd use a web server process (perhaps Node.js itself, perhaps using ExpressJS or Koa though you don't have to) or similar to provide that page in response to a request.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875