I have the following event handler for my html element
jQuery("#seek-bar").click(function(e){
var x = e.pageX - e.target.offsetLeft;
alert(x);
});
I need to find the position of the mouse on the #seek-bar at the time of clicking. I would have thought the above code should work, but it gives incorrect result
Are you trying to get the position of mouse pointer
relativeto element ( or ) simply the mouse pointer locationTry this Demo : http://jsfiddle.net/AMsK9/
Edit :
1)
event.pageX,event.pageYgives you the mouse position relative document !Ref : http://api.jquery.com/event.pageX/
http://api.jquery.com/event.pageY/
2)
offset(): It gives the offset position of an elementRef : http://api.jquery.com/offset/
3)
position(): It gives you the relative Position of an element i.e.,consider an element is embedded inside another element
example :
Ref : http://api.jquery.com/position/
HTML
JavaScript