2

When using jQuery on, is there a way to get the selected element from within the handler, as opposed to the event target? For example, none of the following is related to body:

$('body').on('click', 'h1', function(e){
    console.log(e.target);
    console.log(e.currentTarget);
    console.log(this);
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Hugo Silva
  • 6,748
  • 3
  • 25
  • 42
  • 2
    Well, here is the correct answer: event.delegateTarget http://api.jquery.com/event.delegateTarget/ – jme11 Jun 23 '14 at 23:50

1 Answers1

5

The value of e.delegateTarget in your event handler will be the element that actually handled the event which in your example will be the <body> tag.

As it looks like you already know, e.target will be set to the object that originated the event (might be a child object in your <h1>) and e.currentTarget and this will be set to the object that matches your "h1" selector.

Most of the time, it's the object that matches your <h1> selector that you want, but you can use e.delegateTarget if you want to know which object intercepted the event.

jQuery documentation reference: http://api.jquery.com/event.delegateTarget/

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Not exactly. `e.target` might be set to any element *in the h1* that was actually clicked. – Bergi Jun 24 '14 at 00:38