Posts
read more
The cleanest auto resize for a textarea
The cleanest way to auto-resize a textarea while typing (like wall post on Facebook) is George Papadakis solution. It uses only standard javascript and provides eventlisteners for IE (attachEvent) and other browsers (addEventListener):
window.onload = function () {
var t = document.getElementsByTagName('textarea')[0];
var offset = !window.opera ? (t.offsetHeight - t.clientHeight) : (t.offsetHeight
+ parseInt(window.getComputedStyle(t, null)
.getPropertyValue('border-top-width')));
var resize = function (t) {
t.style.height = 'auto';
t.style.height = (t.scrollHeight + offset) + 'px';
}
t.addEventListener && t.addEventListener('input',
function (event) {
resize(t);
});
t['attachEvent'] && t.attachEvent('onkeyup',
function () {
resize(t);
});
}Other solutions are jQuery.fn.autoResize, jQuery.autogrow, elastic.