Skip to content

Instantly share code, notes, and snippets.

@thorn
Created May 23, 2012 15:28
Show Gist options
  • Save thorn/2775899 to your computer and use it in GitHub Desktop.
Save thorn/2775899 to your computer and use it in GitHub Desktop.
sets cursor position of text areas and fields on selected position
$(function() {
$('textarea').focus().setCursorPosition($('textarea').val().length);
});
$.fn.setCursorPosition = function(pos) {
if ($(this).get(0).setSelectionRange) {
$(this).get(0).setSelectionRange(pos, pos);
} else if ($(this).get(0).createTextRange) {
var range = $(this).get(0).createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
}(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment