$.extend($.fn, {
        selectRange: function(start, end) {
            // use only the first one since only one input can be
focused
            if ($(this).get(0).createTextRange) {
                var range = $(this).get(0).createTextRange();
                range.collapse(true);
                range.moveEnd('character',   end);
                range.moveStart('character', start);
                range.select();
            }
            else if ($(this).get(0).setSelectionRange) {
                $(this).focus().get(0).setSelectionRange(start, end);
            }
            return $(this);
        }
});

I think it's pretty much the best crossbrowser solution you will find.

$('input').selectRange(0,0) to reset the carret position, to position
it at the end you will need to get length of the input's value.. you
just made me think
I could add support for -1, thanks :D

~h

On Jul 2, 2:55 pm, Paul Malan <[EMAIL PROTECTED]> wrote:
> By default it seems browsers select all the text in a textbox when it
> gains focus by way of a tab-press.  I would like the cursor to be
> positioned at the end of any existing text in the input, instead.  The
> examples I'm turning up on Google don't work and seem needlessly
> complex, and since jQuery simplifies everything else I used to hate
> about Javascript, I thought I'd see if there's a simple way to
> position the cursor in a text input box on focus.  Is it doable?
>
> Thanks...

Reply via email to