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

StackOverflow https://stackoverflow.com/questions/18185426

Question

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.

Was it helpful?

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);
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top