k.ken
k.ken

Reputation: 5413

Getting the coordinates of an element relative to page

I was testing out an approach to a solution and found that the methods or functions available for a programmatically triggered JavaScript event is different from ones that are available when the user clicks on the element. For instance, I wasn't able to get the mouse positions of the click event. If the user had clicked, I would have been able to get the mouse positions and a lot more. I am adding the events and triggering it via jQuery. Any reason for this? Any one know how I can get the mouse position after programmatically triggering it using jQuery?

Upvotes: 0

Views: 111

Answers (1)

Jorge Y. C. Rodriguez
Jorge Y. C. Rodriguez

Reputation: 3449

you can use the offset jquery function:

var clickedPostion = $(this).offset();
alert(e.clientX - clickedPostion.left);
alert(e.clientY - clickedPostion.top);

I know the sample it is on click, but you can change it to mousemove()

Here is a working sample:

http://jsfiddle.net/6wccZ/

Upvotes: 3

Related Questions