Skip to content

Instantly share code, notes, and snippets.

@adrienchretien
Created December 8, 2013 09:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrienchretien/7855178 to your computer and use it in GitHub Desktop.
Save adrienchretien/7855178 to your computer and use it in GitHub Desktop.
Resize an element when the visitor create(or remove) a new line on a text field (like a textarea)
function AutoSize(textarea) {
textarea.className += ' js-autosize';
function resize() {
textarea.style.height = 'auto';
var height = textarea.scrollHeight;
if (window.getComputedStyle) {
var styles = window.getComputedStyle(textarea);
height -= parseInt(styles['padding-top']) + parseInt(styles['padding-bottom']);
}
textarea.style.height = height + 'px';
}
if (!textarea.oninput) {
textarea.oninput = function () {
resize();
};
} else {
textarea.onkeyup = function () {
resize();
};
}
return { 'textarea': textarea, 'resize' : resize }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment