Skip to content

Instantly share code, notes, and snippets.

@jawdatls
Created May 11, 2015 08:35
Show Gist options
  • Save jawdatls/e77b467541c9fea075b0 to your computer and use it in GitHub Desktop.
Save jawdatls/e77b467541c9fea075b0 to your computer and use it in GitHub Desktop.
jQuery get cursor position
(function ($, undefined) {
$.fn.getCursorPosition = function() {
var el = $(this).get(0);
var pos = 0;
if('selectionStart' in el) {
pos = el.selectionStart;
} else if('selection' in document) {
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart('character', -el.value.length);
pos = Sel.text.length - SelLength;
}
return pos;
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment