views:

216

answers:

1

Here's the code:

$("#textyt:input").focus(function() {
 $(this).animate({width:"545px"},500).css("color","#614A3E");
 $(this).select();
 $(this).mouseup(function(e){
  e.preventDefault();
 });
});

If I take away the animate effect, this focus event selects the text (as I'd like). With the animate effect, the text deselects when the animation is done in Firefox. This works fine in Safari as is. Is there any way to ensure the text is still selected when the animation finishes in FF? Thanks!

+3  A: 

Try using:

 this.focus();
 this.select();

after the animation.

That would select the text after the animation completes. The width animation works by dynamically changing the CSS width property, and might lose focus in firefox, but what might be a better idea would be to change the width of an container element, not the actual textarea.

$("#textyt:input").focus(function() {
    $(this).animate(
        {width:"545px"}, 500, function(){
            this.focus();
            this.select();
        }).css("color","#614A3E");
     $(this).select();
     $(this).mouseup(function(e){
         e.preventDefault();
     });
});
CodeJoust
....I still can't seem to get this to work. How would I add this code after the animation? The animation continues for 500ms after the select. Could you spell out for me where I would place this in the code above? I also tried applying the animate to the container element in this case, the parent().parent(), but the input box didn't expand with it and even so I still lost focus.
Harold
...answered my own question, sort of. I don't think this is what you had in mind but it works: setTimeout("document.getElementById('textyt').select();",500);
Harold
Not deserving of a new answer, so I edited CodeJoust's example to show you to execute the code after the animation is finished. `$().animate`'s final parameter is an anonymous function that is fired after the animation is finished.
cpharmston
Thanks for your help! That's a much better solution, thanks.
Harold
Thanks or the revision.Question: Why the E.preventDefault handeler?
CodeJoust