Created
January 10, 2013 01:51
-
-
Save clee704/4498718 to your computer and use it in GitHub Desktop.
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
// 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