Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
document.getElementById('#ham') doesn't find the element

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)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda