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

139
Vistas
Why does using "this" on an checkbox event listener not return the dom object, but rather the window object?

I have an event listener on a checkbox that passes a few arguments using "this", however for some reason it doesn't work.

Upon sending "this" to the console, I can see that unlike my other event listeners that I'm using for text input and select nodes, that it's returning the entire window object

The code I'm using:

document.getElementById("nightMode").addEventListener('change', () => {
    console.log(this)
})

What I would expect to see in console:

<input id="nightMode" type="checkbox" />

What is actually being sent to the console:

Window {window: Window, self: Window, document: document, name: '', location: Location, …}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

It's because you're using an arrow function ( () => { ... } ). Replace it with a normal function ( function() { ... } ).

Arrow functions don't have a this of their own. It cannot change; this inside an arrow function will always point to whatever this was at the point in time when the function was declared.

about 4 years ago · Juan Pablo Isaza Denunciar

0

use this code instead:

<input id="nightMode" type="checkbox" onchange="alert(this)">
about 4 years ago · Juan Pablo Isaza Denunciar

0

That's because when you use arrow function, you have binded this of your global scope ( Window ) to your handler function.

Try to use normal function decalration instead: function () { ... }

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