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

193
Vistas
nextElementSibling returns "undefined". Why?

You can see my code below. The style of the text inside the span element is supposed to change after the user checks either one of the checkboxes, but it doesn't. I tried to print the span element to the console after getting it via nextElementSibling and the result was 'undefined'. Why?

const checkBoxes = document.querySelectorAll('input[type=checkbox]');
let j = 0,
  l = checkBoxes.length;
for (j; j < length; j++) {
  checkBoxes[j].addEventListener("change", () => {
    const spanElement = this.nextElementSibling;
    console.log(spanElement);
    if (this.checked) {
      spanElement.style.textDecoration = "line-through";
    } else {
      spanElement.style.textDecoration = "";
    }
  });
};
ul {
  list-style-type: none;
}
<h2>To-Do List</h2>
<input id="input-task" type="text">
<button id="add-task-button">Add Task</button>
<ul id="task-list">
  <li>
    <input type="checkbox">
    <span class="task"> Send email to Bob </span>
    <button class="delete-btn">Delete Task</button>
  </li>
  <li>
    <input type="checkbox">
    <span class="task"> Take vitamin D </span>
    <button class="delete-btn">Delete Task</button>
  </li>
  <li>
    <input type="checkbox">
    <span class="task"> Practice coding problems </span>
    <button class="delete-btn">Delete Task</button>
  </li>

</ul>

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It is because of arrow function which does not have it's own lexical scope, but instead takes on the one where it is defined. Replace it with function syntax and you're good to go!

window.addEventListener('DOMContentLoaded', () => {
const checkBoxes = document.querySelectorAll('input[type=checkbox]');
let j = 0,
  l = checkBoxes.length;
for (j; j < l; j++) {
  checkBoxes[j].addEventListener("change", function(){
    const spanElement = this.nextElementSibling;
    console.log('here', spanElement);
    if (this.checked) {
      spanElement.style.textDecoration = "line-through";
    } else {
      spanElement.style.textDecoration = "";
    }
  });
}
})
ul {
  list-style-type: none;
}
<h2>To-Do List</h2>
<input id="input-task" type="text">
<button id="add-task-button">Add Task</button>
<ul id="task-list">
  <li>
    <input type="checkbox">
    <span class="task"> Send email to Bob </span>
    <button class="delete-btn">Delete Task</button>
  </li>
  <li>
    <input type="checkbox">
    <span class="task"> Take vitamin D </span>
    <button class="delete-btn">Delete Task</button>
  </li>
  <li>
    <input type="checkbox">
    <span class="task"> Practice coding problems </span>
    <button class="delete-btn">Delete Task</button>
  </li>

</ul>

about 4 years ago · Santiago Trujillo 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