8May/093
jQuery Select Text Range
Here's a jQuery function I wrote which you can use to select a range of text in an input field.
$.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();
}
});
};
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();
}
});
};
March 22nd, 2010 - 06:29
Thank for the hint
JQuery rules!
March 22nd, 2010 - 17:26
You’re welcome
Glad you found it useful. jQuery really is nice to work with!
June 30th, 2010 - 17:40
Hey man, grate thanks to your code!
Jquery is really nice for our coding, and your code make my job much easier.