Last active
November 26, 2015 06:00
-
-
Save jobyjoseph/4c8a7c761c1016af94c1 to your computer and use it in GitHub Desktop.
jQuery function to select a range of text inside textarea or textbox
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
$.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