Get cursor position with jquery

Asked

Viewed 1,596 times

1

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

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

Edit: I think I expressed myself badly, I want to get the position of the last character I typed, relative to the screen.

2 answers

1

I hope it helps:

PS: Executes the code and moves 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>
<div id="log"></div>

See the complete example here.

  • this function looks good, but I tried to do in the event "keyup" and it did not work

  • Show me how you’re trying to do it.

1

You can save the mouse information and pick up when calling the 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);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.