Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save antoniobrandao/ef00619c9e29a56ca2d7 to your computer and use it in GitHub Desktop.
Save antoniobrandao/ef00619c9e29a56ca2d7 to your computer and use it in GitHub Desktop.
;(function () {
function domReady (f) { /in/.test(document.readyState) ? setTimeout(domReady,16,f) : f() }
function resize (event) {
event.target.style.height = 'auto';
event.target.style.height = event.target.scrollHeight+'px';
}
/* 0-timeout to get the already changed text */
function delayedResize (event) {
window.setTimeout(resize, 0, event);
}
domReady(function () {
var textareas = document.querySelectorAll('textarea[auto-resize]')
for (var i = 0, l = textareas.length; i < l; ++i) {
var el = textareas.item(i)
el.addEventListener('change', resize, false);
el.addEventListener('cut', delayedResize, false);
el.addEventListener('paste', delayedResize, false);
el.addEventListener('drop', delayedResize, false);
el.addEventListener('keydown', delayedResize, false);
}
})
}());
// Use: <textarea auto-resize></textarea>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment