Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Live click event reporting different source in Firefox vs Chrome

With this HTML

<div>
  <button>
    <img src="https://img.skitch.com/20110912-1m2qj31m7sxmh46uheef63gutu.gif">
  </button>
</div>

and this jQuery

$(document).ready(function() { 
  $("body").live("click", function(event) {
    $("body").append(event.target.tagName);            
  });
});

Why is the event target node in Chrome the image and in Firefox it's the button?

jsfiddle test -> http://jsfiddle.net/MikeGrace/YC5A7/

like image 840
Mike Grace Avatar asked Sep 12 '11 05:09

Mike Grace


1 Answers

This isn't limited to images, of course -- I tweaked your code at http://jsfiddle.net/YC5A7/13/ and got the same result with an ordinary hyperlink.

According to the jQuery docs, event.target "can be the element that registered for the event or a descendant of it." So your results are consistent with the intended purpose of that method.

However, event.currentTarget has the desired result in all browsers: http://jsfiddle.net/YC5A7/16/

like image 186
Blazemonger Avatar answered Nov 15 '22 19:11

Blazemonger