3

I want to get the scrollheight of <textarea> when people changing the content.The problem is when I add the content ,I can get a growing scrollheight ,but when I delete contents,I can't get a Reduced scrollheight。And here is the code

$("textarea[id^='_sub_ialready_']").live("keydown keyup focus blur",
    function()
    {
        if(aobj.minHeight == 0)
        aobj.minHeight = $(this).height();
        if($(this).attr("scrollHeight") > aobj.minHeight)
        {
            if($(this).attr("scrollHeight") > aobj.maxHeight)
            {
                nheight = aobj.maxHeight;
                $(this).css("overflow-Y",'scroll');    
            }    
            else
            {
                nheight = $(this).attr("scrollHeight");
                $(this).css("overflow-Y",'hidden');    
            }
            $(this).height(nheight);
       }
    }
)
ivyandrich
  • 31
  • 1
  • 4
  • Could you specify your css or a little bit more of code so we can understand better what would you wanna do? Also, have you checked out any of these questions about getting scrollHeight?: http://stackoverflow.com/questions/5980150/jquery-js-get-the-scrollbar-height-of-an-textarea http://stackoverflow.com/questions/642353/dynamically-scrolling-a-textarea http://stackoverflow.com/questions/454202/creating-a-textarea-with-auto-resize – elboletaire Dec 30 '11 at 02:31

2 Answers2

2

What you want to do is set height to auto first, like so:

$('#textarea').height('auto');

Then get the scrollHeight property:

var h = $('#textarea').prop('scrollHeight');
Elmer
  • 135
  • 1
  • 7
0

What about...

.attr('scrollHeight')

OR

$('textarea:first').get(0).scrollHeight

OR (returns an integer in pixels)

textarea.scrollHeight
Braunson
  • 717
  • 2
  • 12
  • 36