Skip to content

Instantly share code, notes, and snippets.

@mafice
Created August 25, 2012 06:42
Show Gist options
  • Save mafice/3461645 to your computer and use it in GitHub Desktop.
Save mafice/3461645 to your computer and use it in GitHub Desktop.
get cursor X-Y position in textarea
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<p style="font-weight:bold" id="debug"></p>
<textarea style="font-size:15px;"></textarea>
<script>
$(document.body).ready(function(){
$("textarea").keyup(function(event){
y = 0;
_index = 0;
for(
index=0;
(index = $(this).val().indexOf("\n", index)) != -1 &&
index < $(this)[0].selectionStart ;
index++, y++, _index = index
);
x = $(this)[0].selectionStart - _index;
$("#debug").html("(Y, X) = " + "(" + y + ", " + x + ")");
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment