Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript detect scrollbar in textarea

Tags:

I was wondering if anybody knows how I would go about detecting when the scrollbar appears inside a textarea.

I am currently using mootools for my JavaScript and I am having issues getting it to detect a scrollbar.

like image 400
Jamie Avatar asked Jul 13 '10 15:07

Jamie


2 Answers

function has_scrollbar(elem_id) {     const elem = document.getElementById(elem_id);     if (elem.clientHeight < elem.scrollHeight)         alert("The element has a vertical scrollbar!");     else         alert("The element doesn't have a vertical scrollbar."); } 

See this jsFiddle http://jsfiddle.net/qKNXH/

like image 92
Tommaso Taruffi Avatar answered Jan 11 '23 23:01

Tommaso Taruffi


I made a jQuery "compatible" version of Tommaso Taruffis code

function resize_until_scrollbar_is_gone(selector) {      $.each($(selector), function(i, elem) {         while (elem.clientHeight < elem.scrollHeight) {             $(elem).height($(elem).height()+5);         }     }); } 

It can handle multiple elements and accepts: selectors, jQuery objects, or DOM elements.

It can be called like this:

resize_until_scrollbar_is_gone('textarea'); 
like image 44
powtac Avatar answered Jan 11 '23 23:01

powtac