#getDocumentById error

9 messages · Page 1 of 1 (latest)

formal wharf
#

Hello,

This is probably a beginner question, but I couldn't find it the answer. I'm trying to use the function getElementById. Usually this is done on a document, ie: document.getElementById(id). This doesn't work for me it says: "getElementById is not defined.

I googled it, and one of the reasons I found was that I'm using node.js. I installed node.js so I could run my code in visual code studios and see if there's any problems.
Apparently, node.js does not access the document object. And I'm not entirely sure what that means. It advises me to use a work around. I could use queryselector perhaps, but that also works on document.

How can I get getElementbyId working, or perhaps someone has an alternative I'm not aware of?

Thank you.

silent shuttle
#

We need some extra context here. Where/how are you trying to use this method? Some screenshots of your code would help here.

formal wharf
#

Apologies, is this helpful?

silent shuttle
#

@formal wharf could you show your index.html file too please

balmy wigeon
#

Node.js is a "server" environment and has no concept of a document... you cannot run browser-based code on node... I presume you're trying to run your code something like:

node index.js

#

In order to run your browser-based js is inside an actual browser attached to an .html file. So you need to open your .html file in your browser either direction using file:// OR by using some form of server (e.g. LiveServer)

formal wharf
#

Here's my HTML. @balmy wigeon thank you. I'm still a bit confused, I was under the impression that if i installed github desktop and coupled it to Netlify I'd be able to write some code run it on Netlify, to see the results, so I can practice.

Since that isn't the case, should I try the LiveServer route or the file:// route? I'm unfamiliar with either so any tips would be appreciated.

balmy wigeon
#

I've never attempted github desktop with netlify like you mention so I wouldn't claim it's not possible - but I don't know. All I wanted you to know was that it sounded like you were trying to run a browser .js file directly inside node - which you cannot do.

There are lots of ways to accomplish what you want but I personally just use the LiverServer extension for VS Code - it gives a no-fuss way to run vanilla JS + html that's quick, easy, convenient. If you're a relative beginner it's all you'll need for awhile (although there ARE alternatives so no haters please!) until you get into the need for bundlers etc... if/when you get to React for example...

Just search for the extension in the VS Code extensions marketplace and install it there. You CAN as an alternative install it as a command-line tool via npm but I find the extension way handier.