Get cursor position with jquery

1

Is there any way to get the position of the cursor relative to the screen when typing in a textarea?

Ex: I type some character, call a jquery keyup function and get the position where the cursor is stopped, relative to the screen?

Edit: I think I mispronounced, I want to get the position of the last character I typed in relation to the screen.

    
asked by anonymous 18.02.2016 / 16:29

2 answers

1

I hope it helps:

PS: Run the code and move the mouse in the gray area of execution.

$( document ).on( "mousemove", function( event ) {
  $( "#log" ).text( "pageX: " + event.pageX + ", pageY: " + event.pageY );
});
body {
    background-color: #eef;
  }
  div {
    padding: 20px;
  }
<script src="https://code.jquery.com/jquery-1.10.2.js"></script><divid="log"></div>

See the full example here .

    
18.02.2016 / 16:41
1

You can save your mouse information and pick up when you call keyUp:

var posicaoMouse = { x: -1, y: -1 };
$(document).mousemove(function(event) {
    posicaoMouse.x = event.pageX;
    posicaoMouse.y = event.pageY;
});
$( "#campo" ).keyup(function() {
    console.log(posicaoMouse);
});
    
18.02.2016 / 17:00