Created
July 11, 2013 16:28
-
-
Save monolithed/5976965 to your computer and use it in GitHub Desktop.
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
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