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

135
Vistas
This and event undefined inside onclick handler

I have an element with an onclick event (set aside the best practice advice for now):

<div onclick="myfunc()">

in the function:

function myfunc(e) {
  console.log(e);

e is undefined. If I put this in the console.log, this is undefined

My understanding is that the event should be passed to the handler so that code should output the event object to the console, and this should refer to the div element.

This seems very basic and I am not finding any explanations for why this might happen.

Second EDIT: The comments resulted in my function working, but I still have no answer for why "this" is undefined in the function. I get the result with event.currentTarget, but this throws an error.

EDIT:

Based on the colloquy with Pointy, I put "myfunc(event)" and made the corresponding changes to the myfunc declaration, and console.log printed the event:

PointerEvent {isTrusted: true, pointerId: 1, width: 1, height: 1, pressure: 0, …}

Ok, good, BUT when I look at the listing, it says

currentTarget: null

Shouldn't currentTarget be the div event? Also, this is still undefined, which makes sense if currentTarget is null, but everything I read says that in a handler created that way - directly in the element, that this should refer to the element.

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

0

The answers are in the comments, thanks to @Pointy, @Luke Trenaman and @Barmar

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