JavaScript/webpack - issue with event listener in my index.js file
I am working on the Restaurant Page project from The Odin Project (https://www.theodinproject.com/lessons/node-path-javascript-restaurant-page). I'm new to using webpack and keep running into issues. The latest one is that the event listener in my
renderNavbar()
prevents the page from rendering, even with Live Server on. If anyone can help, I'd greatly appreciate it. My code is in the src directory of my repo: https://github.com/savvystrider/webpack-restaurant-pageGitHub
GitHub - savvystrider/webpack-restaurant-page: Odin Project
Odin Project. Contribute to savvystrider/webpack-restaurant-page development by creating an account on GitHub.
14 Replies
can you press f12 and share the errors you get in the console?
also, you didn't set your public path
The error is: Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
What do you maen by public path?
i was wrong, you have this: https://github.com/savvystrider/webpack-restaurant-page/blob/main/webpack.config.js#L7C6-L7C6
GitHub
webpack-restaurant-page/webpack.config.js at main · savvystrider/we...
Odin Project. Contribute to savvystrider/webpack-restaurant-page development by creating an account on GitHub.
im sure the error has A LOT more than just that
you're trying to get an element that doesn't exist
https://github.com/savvystrider/webpack-restaurant-page/blob/main/src/index.js#L9
^ the
renderNavbar
is defined here
can i copy and paste the contents of that file here?Sure, do I need to enable that?
nah
can you see the last line?
NOTHING exists in the body until the function
body
returns
and inside renderNavbar
you are doing document.getElementById("menu-link")
Ah ok, I need to actually render an element to the DOM before I can attach an event listener
you're trying to get an element that doesn't exist at all
no
there's a way of doing this:
instead of doing this:
you could do this:
the idea is simple: you don't need to have an element in the body to give it an event handler
Oh wow, didn't know I could do that
Made the update as you suggested, ran the build, and it works as expected. Thank you so much!
you're welcome
this is something that i don't often see explained in tutorials and things like that
if i were you, i wouldn't do the way you're doing things
for learning, this is perfectly fine, and will work as you want
but instead of always generating 100% of the document in javascript, you should consider having the html in the html file, and then you can add the javascript functionality to do the stuff you need
the disadvantage of your method is that if something goes wrong, there's nothing on the page
lol I mainly wanted some experience with webpack, didn't realize how complicated this would get. Definitely don't plan on on setting up another project like this unless it's suited for it
you're still on time to change it
i understand that this was done for learning webpack and stuff
but, honestly, what you've done so far requires 0 lines of javascript to work, so, it wouldn't be an impressive exercise
besides practicing, your solution has the advantage of everything being done in places you can easily change