Search code examples
javascriptjquerycursor-position

How to find pageX-pageY positions of the cursor (when inside a textbox) using javascript / jquery?


I need to find the pageX and pageY positions of the cursor within a textbox on any event, such as keyup.

<input type="text" id="addOwner">

$("#addOwner").keyup(function(event) {
    var pageY = event.pageY;  // currently getting Undefined
    var pageX = event.pageX;  // currently getting undefined  
})

Here is a fiddle.


Solution

  • Check jQuery offset() to find out the text box position. DEMO

    $("#addOwner").keyup(function(event) {
        var offset = $(this).offset();
        var pageY = offset.left;  // currently getting Undefined
        var pageX = offset.top;  // currently getting undefined 
        alert(pageY);
        alert(pageX);
    })