Skip to content

Instantly share code, notes, and snippets.

@Loiree
Created October 9, 2015 17:50
Show Gist options
  • Save Loiree/e2820847b7dda431f3b0 to your computer and use it in GitHub Desktop.
Save Loiree/e2820847b7dda431f3b0 to your computer and use it in GitHub Desktop.
Позиция курсора
function doGetCaretPosition (ctrl) {
var CaretPos = 0; // IE Support
if (document.selection) {
ctrl.focus ();
var Sel = document.selection.createRange ();
Sel.moveStart ('character', -ctrl.value.length);
CaretPos = Sel.text.length;
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == '0')
CaretPos = ctrl.selectionStart;
return (CaretPos);
}
function setCaretPosition(ctrl, pos){
if(ctrl.setSelectionRange)
{
ctrl.focus();
ctrl.setSelectionRange(pos,pos);
}
else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
// где ctrl — Textarea object
// link — http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment