Program & Design Tips, tricks, tutorials, and tools on programming & web design

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();
        }
    });
};
Tagged as: Leave a comment
Comments (3) Trackbacks (0)
  1. Thank for the hint :-) JQuery rules!

  2. You’re welcome :) Glad you found it useful. jQuery really is nice to work with!

  3. Hey man, grate thanks to your code!
    Jquery is really nice for our coding, and your code make my job much easier.


Leave a comment


No trackbacks yet.