Ick! Global variables and eval'd code! How about (untested, logic should be
unchanged):

$(function() {
    $(':text').bind('focus', function() {
        var o = this;
        if(o.setSelectionRange) {     /* DOM */
            setTimeout(function()
{o.setSelectionRange(o.value.length,o.value.length);}, 2);
        } else if(o.createTextRange) {    /* IE */
            var r = o.createTextRange();
            r.moveStart('character', o.value.length);
            r.select();
        }
    });
});

Adds an anonymous function, which adds a function call, but saves an eval
and doesn't require a global variable, which is potentially problematic.

--Erik



On 7/2/08, Brian J. Fink <[EMAIL PROTECTED]> wrote:
>
>
> And I checked my code again. It DOES work on FF3, FF2, and IE7.
>
>
> On Jul 2, 5:37 pm, Paul Malan <[EMAIL PROTECTED]> wrote:
> > Thanks for the code.  It doesn't work for me--still when I tab into a
> > textbox in either IE7 or FF3 the content is selected and the cursor
> > isn't positioned at the end of the text, even when I pull out
> > everything but this function and two textboxes to test.
> >
> > I think I may just give up--it was a minor detail and not requested by
> > the users.  Bugs me, though.  Seems like it shouldn't be so tricksy...
> >
> > On Jul 2, 3:12 pm, "Brian J. Fink" <[EMAIL PROTECTED]> wrote:
> >
> > > There may be a jQuery way to do this, but I don't know what it is.
> >
> > > However, I do know 2 ways to accomplish this: one DOM way, one IE way.
> > > Both methods must be employed.
> >
> > > $(function() {
> > >  $('input[type="text"]').bind('focus',function() {
> > >   window.o=this;
> > >   if (o.setSelectionRange)     /* DOM */
> > >    setTimeout('o.setSelectionRange(o.value.length,o.value.length)',2);
> > >   else if (o.createTextRange)     /* IE */
> > >   {
> > >    var r=o.createTextRange();
> > >    r.moveStart('character',o.value.length);
> > >    r.select();
> > >   }
> > >  });
> >
> > > });
> >
> > > 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