$('#contact-form').on( 'change keydown keyup paste cut', 'textarea', function () {
$(this).height(0).height(this.scrollHeight+2);
if ($(this).height() >= 300) {
$('textarea#message').css("overflow", "auto");
}
else {
$('textarea#message').css("overflow", "hidden");
}
}).find('textarea#message').change();
/*
alternative way of doing it
var observe;
if (window.attachEvent) {
observe = function (element, event, handler) {
element.attachEvent('on'+event, handler);
};
}
else {
observe = function (element, event, handler) {
element.addEventListener(event, handler, false);
};
}
var text = document.getElementById('text');
function resize () {
text.style.height = 'auto';
text.style.height = (text.scrollHeight+20)+'px';
}
function delayedResize () {
window.setTimeout(resize, 0);
}
observe(text, 'change', resize);
observe(text, 'cut', delayedResize);
observe(text, 'paste', delayedResize);
observe(text, 'drop', delayedResize);
observe(text, 'keydown', delayedResize);
text.focus();
text.select();
resize();
*/