tags:

views:

27

answers:

3

Hi folks, i would like to expand the height of the box when it is clicked on.

 $(this).find("textarea").css("height","2.6em").focus();

The code above expands the box, but does so abruptly.

How can i make it do so in a sliding manner that is much more elegant?

+2  A: 

You can use .animate() instead of .css(), like this:

$(this).find("textarea").animate({ height: "2.6em" }).focus();

Some browsers lose the focus on animation though, I recall from a question a few weeks back, you may need to do this instead:

$(this).find("textarea").animate({ height: "2.6em" }, function() {
  $(this).focus();
})
Nick Craver
Nice, thanks! One side effect of this is that the effect still continues to happen, even when it is already in focus. how do i cancel the effect when it is in focus? =)
ming yeow
@ming - I'm not sure what context this is inside of, where is this code running?
Nick Craver
+1  A: 

Take a look at jQuery's animate function:

$(this).find("textarea").animate({
    height: "2.6em"
  }
JasCav
+1  A: 

The size effect will do this smoothly. Animate, also.

$(this).find("textarea").effect("size", { to: {height: 2.6em} }, 1000).focus();;
Dustin Laine