0

Possible Duplicate:
getting the X/Y coordinates of a mouse click on an image with jQuery

I want to calculate clicked position of a image, lets say this will be an example:

enter image description here

And if I would click on it somewhere, I want to display an alert with the X/Y position of the clicked area on the image, there are pixels on the images, so it should display x pixel offset and y pixel offset.

Is that possible to do with the Javascript? Any examples would be appreciated.

Community
  • 1
  • 1
Scott
  • 5,991
  • 15
  • 35
  • 42

2 Answers2

0

Using JQuery, use the pageX and pageY values of the event to which you substract to the offset of the element.

http://jsfiddle.net/BramVanroy/D63KS/1/

$(document).ready(function(){
    $('img').mousedown(function(e){
        var offset = {
            x: e.pageX - $(this).offset().left,
            y: e.pageY - $(this).offset().top
        }
            $('#info').html('{'+offset.x+'; '+offset.y+'}');
    });
});​
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
Bali Balo
  • 3,338
  • 1
  • 18
  • 35
0

You can do something like this to get x and y, where 'theimage' is the id of your image

$("#theimage").live("click",function(e){
e.preventDefault();
    var x = e.pageX - $(this).offset().left;
    var y = e.pageY - $(this).offset().top;


});
Joe M
  • 2,527
  • 1
  • 25
  • 25