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

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

I'm trying to create a simple todo list app as part of an online tutorial by DevEd, but am getting stuck on one seemingly puzzleing error.

Using the following HTML markup:

<div class="todo-container">
  <ul class="todo-list"></ul>
</div>

.. alongside some javascript to create and insert some list elements, I'm then using the following function to filter by todo list entries that have a specific class tag.

function filterTodo(e) {
  const todos = todoList.childNodes;
  todos.forEach(function (todo) {
    switch (e.target.value) {
      case "all":
        todo.style.display = "flex";
        break;
      case "completed":
        if (todo.classList.contains("completed")) {
          todo.style.display = "flex";
        } else {
          todo.style.display = "none";
        }
        break;
    }
  });
}

All the above seem to work together fine, until I added ANYTHING, even a comment line in between the <ul></ul> tags like below:

<div class="todo-container">
      <ul class="todo-list">
         <!-- boo -->
      </ul>
</div>

Upon doing this I get the following error when trying to filter the entries:

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

Can anyone please explain?

Full code found here: https://github.com/developedbyed/vanilla-todo (not my repo)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

childNodes returns a collection of - as it sounds like - child nodes. Some of these nodes may not be elements, but text nodes - and text nodes have no classList property. For example:

const childNodes = document.querySelector('.container').childNodes;
console.log(childNodes[0].classList);
console.log(childNodes[1].classList);
<div class="container">text node<span>element</span></div>

Use .children instead, which will retrieve only child elements.

function filterTodo(e) {
  [...todoList.children].forEach(function (todo) {
about 4 years ago · Juan Pablo Isaza Denunciar
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