JavaScript Get mouse click position within an element

Older browsers such as IE 7 & 8 use different event properties to reveal the mouse click coordinates within an element.
$('img').click(function(e) { 
	var position = 'X: ' + (e.layerX || e.offsetX) + ' Y: ' + (e.layerY || e.offsetY);
	alert('Click position within image: ' + position);
});
Posted by
Snippet Viewed 4282 times.

Share your JavaScript code snippets:

  • Get some recognition & a link back to your site.
  • Create your own code library.
  • Help your fellow developers, as they have helped you.
Submit | Browse

Most Recent javascript snippets

Most Viewed

The FitDesk Under Desk Elliptical