Skip to content

Instantly share code, notes, and snippets.

@monolithed
Created July 11, 2013 16:28
Show Gist options
  • Save monolithed/5976965 to your computer and use it in GitHub Desktop.
Save monolithed/5976965 to your computer and use it in GitHub Desktop.
var cursor_position = {
get: function(input) {
if (input === null)
return 0;
var position = 0;
if ('selectionStart' in input || input.selectionStart == 0) {
position = input.selectionStart;
}
else if (document.selection) {
input.focus();
var selection = document.selection.createRange ();
selection.moveStart ('character', -input.value.length);
position = selection.text.length;
}
return position;
},
set: function(input, position) {
if (input === null)
return 0;
if (input.createTextRange) {
var range = input.createTextRange();
range.move('character', position);
range.select();
return true;
}
else {
if (input.selectionStart || input.selectionStart === 0 /* FF */) {
input.focus();
input.setSelectionRange(position, position);
return true;
}
else {
input.focus();
return false;
}
}
}
}
/*
var input = document.getElementById('input');
cursor_position.get(input)
cursor_position.set(input, 10);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment