Created
May 23, 2012 15:28
-
-
Save thorn/2775899 to your computer and use it in GitHub Desktop.
sets cursor position of text areas and fields on selected position
This file contains hidden or 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() { | |
$('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