The below code will give you the X, Y coordinates of a mouse click within a given box. Removing all the stuff about the offset, you can easily get the X, Y coordinates of the click relative to the browser window.
$(function() {
$("#demo-box").click(function(e) {
var offset = $(this).offset();
var relativeX = (e.pageX - offset.left);
var relativeY = (e.pageY - offset.top);
alert("X: " + relativeX + " Y: " + relativeY);
});
});
Example
Click in the box below
Very cool trick!
I like thisone. It’s very usefull in future projects.
I like it too, very simple and works well.
thanks
VERY COOL TIP THANKS CHRIS YOU ARE M3ALLEM (MEANS master in arabic)
You’re the bomb! Just what I needed.
give some xplanation
Works great in IE, but not in Safari. :-(
Hi,
It’s realy nice. how i can get it in percentage.
thanks.
uhmm try x pos/width of box
and y pos/height of box
how to get it in percentage ?
X Position / Width and Y Position / Height.
Hey man, great as usual but there’s an error with jQuery not being declared, I think.
Is this still best practice?