davesmiths
7/11/2014 - 2:58 PM

Resize a textarea to the full height of its contents (js)

Resize a textarea to the full height of its contents (js)

if (el.scrollHeight !== undefined && el.clientHeight !== undefined) {

  //Reset the height
  el.style.height = '1px';

  // Set height to the scrollHeight
  el.style.height = el.scrollHeight +'px';
  
  // We now have a clientHeight to work with
  // if there is a horizontal overflow then clientHeight will be different to scrollHeight
  el.style.height = el.scrollHeight + el.scrollHeight - el.clientHeight + 'px';
  
}