Whenever I add an addEventListener, or a toggle in javascript, I get ''can't read **** of null" or ''can't read of undefined". I tried many different ways to solve it, deleting the javascript file and recreating it, putting everything inside an "addEventListener('DOMContentLoaded, ...'), moving the javascript code in html file. Nothing works, I don't know why. I tried all the ways that they say on the internet that work.
const hamn= document.getElementById('#ham')
const navLinks= document.getElementById('#navLink')
function openMenu() {
navLinks.classList.toggle('hidden')
}
hamn.addEventListener('click', openMenu)
getElementById already knows you're going to pass an id (hence the ById in the name!), so you need to remove the # from the string passed to it. The # is only used in CSS to express an id selector.
You'd only need the # if, instead of getElementById, you were using querySelector(cssSelectorString) which expects you to pass a valid CSS selector.
const hamn= document.getElementById('ham')
const navLinks= document.getElementById('navLink')
function openMenu() {
navLinks.classList.toggle('hidden')
}
hamn.addEventListener('click', openMenu)