1

HTML:

<div class="parent">
     <div class="child"></div>
</div>

jQuery:

$(".parent").on('click', callback);

The problem is that depends on the styling (specifically where the mouse pointer clicks) the click event is fired on .child or .parent depending on the location of the mouse.

Is there a way of getting the event always from .parent even if it was clicked on .child? How I used to solve this issue is by checking the id or class of $(event.target) and if it was not the .parent then I would do $(event.target).parent() but I need a solution that works for several scenarios rather than a fix.

Jad Joubran
  • 2,511
  • 3
  • 31
  • 57

3 Answers3

3

Use event.currentTarget instead of event.target. The event.currentTarget property is the current DOM element within the event bubbling phase, and is typically equal to this.

event.currentTarget - The current DOM element within the event bubbling phase.

event.target - The DOM element that initiated the event.

$(".parent").on('click', callback);

function callback(event){

    alert( event.currentTarget === this );
}

fiddle for event.currentTarget - FIDDLE

fiddle for event.target - FIDDLE2

Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
1

Check if target is currentTarget.

if (event.target == event.currentTarget)

http://jsbin.com/jehuz/1/edit

Javascript :

$(".parent").on('click', function (event) {
    if (event.target == event.currentTarget) return;

    $(event.currentTarget);
}

Sample javascript :

$(".parent").on('click', function (event) {
    $("p.1").text("target : "+event.target.className);
    $("p.2").text("currentTarget : "+event.currentTarget.className);
    if (event.target == event.currentTarget) {
        $(this).toggleClass("a");
    }
});

https://i.stack.imgur.com/lddLv.png

user2226755
  • 12,494
  • 5
  • 50
  • 73
  • Thank you! All I need is to do this: `$(event.currentTarget)` can you update you answer? – Jad Joubran Jul 08 '14 at 09:49
  • ` if (this == event.currentTarget) return;` I think this is wrong.. we don't even need to do a check no? – Jad Joubran Jul 08 '14 at 09:53
  • @JadJoubran if (this == event.currentTarget) it is right it check the current element and the clicked one. if any doubt regarding this see my fiddle – Sudharsan S Jul 08 '14 at 09:55
  • yeah but no need to check if this === event.currentTarget because I need both events on the parent and on the child, but I always want to get a revence of the event.currentTarget (the .parent in the example) that's why your solution was correct – Jad Joubran Jul 08 '14 at 09:57
  • `1` and `2` not valid class names. [What characters are valid in CSS class selectors?](http://stackoverflow.com/questions/448981/what-characters-are-valid-in-css-class-selectors) – t.niese Jul 13 '14 at 09:27
0

Keep this code as it is:

HTML:

<div class="parent">
     <div class="child"></div>
</div>

jQuery:

$(".parent").on('click', callback);

but add:

$('.child').click(function() {
   //find the parent of the clild
   var parent = $(this).closest('.parent');
   //if it exists
   if (parent.length > 0)
   {
      //fire the click event, above, thus running 'callback'
      parent.click();
   }
});

That said this does sound like an XY Problem

Community
  • 1
  • 1
Liam
  • 27,717
  • 28
  • 128
  • 190