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

320
Visualizações
Uncaught TypeError: Cannot read properties of undefined (reading 'contains') JavaScript error

The following code is producing an error for me

tab.addEventListener("click", (e) => { 
  e.preventDefault();
  if (document.getElementsByTagName("a").classList.contains("active")) {
    document.getElementsByTagName("a").classList.remove("active"); 
  } else {
    console.log("nothing")
  }
}) 

It is producing this error in the console.

Uncaught TypeError: Cannot read properties of undefined (reading 'contains')

I am confused, because I am not sure why this error is happening. Even stranger, when the classList.remove("active") is outside of the conditional, the same error occurs, with contains replaced with add. Can someone explain to me why this error is happening?

Thank you

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

document.getElementsByTagName("a")

returns an array

.classList will be undefined on an array

So when you try to access the contains() method you get the error that you are seeing.

A solution would be to use a filter and for each on the array.

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