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

249
Visualizações
Cannot read properties of undefined (reading 'classList') while class has been declared

I'm running into an issue when I try to add a classname to the parent div when the child has of the parent has a class name of 'full-width'

The following code and markup is used

JS

const child = document.getElementsByClassName('full-width');
child.parentElement.classList.add('full-width-container');

HTML

<div class="wp-container-10 wp-block-columns">
    <div class="wp-container-9 wp-block-column full-width has-green-background-color has-background">
         <!-- content -->
    </div>
</div>

However I do get an error in my console saying

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

When searching for this error it mostly occurs when trying to access the classList property on a null value. However this isn't true when I log child.

console.log(child) returns

HTMLCollection []
0: div.wp-container-9.wp-block-column.full-width.has-green-background-color.has-background
length: 1
[[Prototype]]: HTMLCollection

Also the JavaScript is loaded at the end of the page after the HTML which has the class name.

I really don't get it why I'm still getting this error and why this code doesn't work. Any help is much appreciated.

about 4 years ago · Juan Pablo Isaza
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