Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery / JS get the scrollbar height of an textarea

I've got a textarea with a fixed height. When the user types text into the textarea a scrollbar will show up after the user typed some text in it.

How can I get the scrollbar height using jQuery or plain JavaScript? I've been searching for this for hours, but couldn't find anything. I can't just insert a div and get the scrollbar height via the div offset because a textarea is not allowed to have child elements.

Please don't give me a link to a jQuery Plug-In that does the job. I want to learn something.

like image 260
iWeb Avatar asked May 12 '11 15:05

iWeb


People also ask

How can I get scroll height of textarea?

The scrollHeight property is used to return the entire height of an element including padding in pixels. This will make the textarea height equal to the height of the whole text area, effectively resizing the textarea to fit the text. The 'input' event is fired whenever the value of an input or textarea changes.

How do I get the height of my scroll bar?

Height of scrollbar = offsetHeight – clientHeight.


2 Answers

textarea.scrollHeight

returns an integer (pixels)

like image 58
kennebec Avatar answered Oct 05 '22 14:10

kennebec


$.each($("textarea"), function () {
    var scrollHeight = parseInt(this.scrollHeight);
    if ($("this").val() != "" && isNaN(scrollHeight) == false && scrollHeight > 0 && scrollHeight > $(this).height()) {
        console.log($(this).attr("id"));
        $(this).height(scrollHeight);
    }
});
like image 39
Kashyap Makadia Avatar answered Oct 05 '22 16:10

Kashyap Makadia