dropshado.ws

Front-end development minutiæ

by David DeSandro

Archive
RSS feed
Twitter
original Tumblr version

event.currentTarget vs. this

You can use event.currentTarget in place of this inside jQuery event handlers

$('.button').on( 'click', function( event ) {
  console.log( event.currentTarget === this ); // true
});

While this inside event handlers has become commonplace, I feel the event.currentTarget pattern is more obvious. It is more compatible working with other patterns that use this differently.

04 Nov 2015
  • jQuery
  • JavaScript
Page of
  • desandro.com
  • @desandro
  • github.com/desandro
  • desandro’s JS Fiddles