Skip to content

Instantly share code, notes, and snippets.

@clee704
Created January 10, 2013 01:51
Show Gist options
  • Save clee704/4498718 to your computer and use it in GitHub Desktop.
Save clee704/4498718 to your computer and use it in GitHub Desktop.
// Stolen from http://stackoverflow.com/a/841121/332370
// Modified so that:
// 1. `end' can be omitted (defaults to the same value as `start').
// 2. -1 means the end of the range.
$.fn.selectRange = function(start, end) {
return this.each(function() {
if (typeof end == "undefined") {
end = start;
}
if (start == -1) {
start = this.value.length;
}
if (end == -1) {
end = this.value.length;
}
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();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment