-
-
Save kulakowka/8c6e7fe5007256477752 to your computer and use it in GitHub Desktop.
Textarea auto-resize vanilla javascript (no jQuery plugin), for modern browsers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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