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.
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.
Height of scrollbar = offsetHeight – clientHeight.
textarea.scrollHeight
returns an integer (pixels)
$.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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With