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

138
Vistas
Unable to capture the closest span element on mouse click

The MDN specification of the closest method reads that the method traverses the DOM upwards, in the direction to the document root. But what method does one need to use in my case instead of closest then?

In my HTML page I have the following code:

<div class="slider__steps">
  <span></span>
  <span></span>
  <span class="slider__step-active"></span>
  <span></span>
  <span></span>
</div>

In my JS file I want to capture the closest span element to a mouse click and change its CSS class. I try to do this as follows:

moveSlider(elem) {
    let sliderSteps = elem.querySelector('.slider__steps');
    let spans = sliderSteps.querySelectorAll('span');
    elem.addEventListener('click', moveThumb);
    function moveThumb(event) {
      for (let span of spans) {
        span.classList.remove('slider__step-active');
      }
      event.target.closest('span').classList.add('slider__step-active');
    }
  }

But the error I get is

Uncaught TypeError: event.target.closest(...) is null

How does one actually catch these spans on mouse clicks?

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