Created
August 25, 2012 06:42
-
-
Save mafice/3461645 to your computer and use it in GitHub Desktop.
get cursor X-Y position in textarea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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