Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

257
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda