Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery animate attribute?

I use something like this, for auto growing (auto size) textarea:

$('textarea').keyup(function() {
    $(this).attr('rows', $(this).val().split("\n").length);
});

How to add smooth animation to auto growing for code above?

Function .animate() with rows doesn't work.

like image 748
kicaj Avatar asked Feb 15 '26 01:02

kicaj


1 Answers

It would be better to use height CSS. Your code only works if the textarea is set to not wrap text.

Try something like this:

$('textarea').keyup(function() {
    this.style.height = (this.scrollHeight+8)+"px";
});

Then, if you have suitable transition properties set on the textarea, it will animate.

like image 148
Niet the Dark Absol Avatar answered Feb 16 '26 15:02

Niet the Dark Absol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!