8
<body>
<script type="text/javascript">$(function(){
$("textarea").live("keyup keydown",function(){var h=$(this);
h.height(h[0].scrollHeight);
});});

</script>
<textarea style="resize:none;width:760px;height:60px; overflow:hidden;" ></textarea>
</body>

When textarea get overflowed it get scrollbar and scrollheight which gets applied to its height but not working for decreasing textarea height as on decreasing its value length it doesnot get scrollbar

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191

3 Answers3

25

The minimum scrollHeight of a textarea is always going to be the height. To get an accurate scrollHeight, set the height to 1 first.

h.height(1).height(h[0].scrollHeight);

http://jsfiddle.net/aarongloege/t2vAr/

0

If you do not want to code it by yourself, you can also use this script: https://github.com/brandonaaron/jquery-expandable It already contains a nice slide-effect ;-)

Christopher
  • 2,005
  • 3
  • 24
  • 50
0

Pure JavaScript version of the above code

textarea.style.height = '0';
textarea.style.height = textarea.scrollHeight + 'px';
live627
  • 397
  • 4
  • 18