Yahoo! I have found a solution! Your example really intrigued me, so I decided to play jsFiddle to try and fix your flashing problem . The blinking is due to the fact that TextArea has "a lot of text" and scrolling occurs. The keyup event keyup not equipped to beat this scrollbar, but ... there is a scroll event!
Html:
<textarea id="tst" rows="1" cols="40">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque interdum porttitor neque eget consequat. Sed faucibus purus vitae felis facilisis bibendum.</textarea>
Css:
textarea{ overflow:hidden; overflow-x:hidden; overflow-y:hidden; padding:10px; }
To perform the increase, I use the rows attribute in TextArea. I wrote it in a function: I increase the size of the area with this function:
//resize text area function resizeTextArea(elem){ elem.height(1); elem.scrollTop(0); elem.height(elem[0].scrollHeight - elem[0].clientHeight + elem.height()); }
Now we only need to bind the size:
//inital resize - on document load resizeTextArea($('#tst')); //bind events $('#tst').bind('scroll keyup', function(){ resizeTextArea($(this)); });
Note: no flashing! What for? Because using the scoll event! You can try the solution here: http://jsfiddle.net/KeesCBakker/2D8Kf/
Good luck
Change Changed code in jsFiddle example to support schemes with dynamically added text areas. Check also this SO question .
Kees C. Bakker
source share