Created
May 11, 2015 08:35
-
-
Save jawdatls/e77b467541c9fea075b0 to your computer and use it in GitHub Desktop.
jQuery get cursor position
This file contains 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
(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