Skip to content

Instantly share code, notes, and snippets.

@jobyjoseph
Last active November 26, 2015 06:00
Show Gist options
  • Save jobyjoseph/4c8a7c761c1016af94c1 to your computer and use it in GitHub Desktop.
Save jobyjoseph/4c8a7c761c1016af94c1 to your computer and use it in GitHub Desktop.
jQuery function to select a range of text inside textarea or textbox
$.fn.selectRange = function (start, end) {
return this.each(function () {
if (this.setSelectionRange) {
this.focus();
this.setSelectionRange(start, end);
} else if (this.createTextRange) {
var range = this.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
}
});
};
$('textarea').selectRange(3,5); // Selects text from position 3 to 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment