Wednesday, September 1, 2010

Jquery Tip: How to get mouse cursor position

In this jQuery tip post, We will see how can we get the mouse cursor position using jQuery. See below jQuery code.
$(document).ready(function(){
  $(document).mousemove(function(e){
     $('#spnCursor').html("X Axis : " + e.pageX + " Y Axis : " + e.pageY);
  });
});
When the DOM is ready, we listen for mousemove event. Whenever user moves the mouse then this event gets called and we bind the pageX and pageY property to the span element.
See live Demo and Code.
Don't forget to read:
Feel free to contact me for any help related to jQuery. I will gladly help you.