Skip to content

Instantly share code, notes, and snippets.

@luokebi
Last active August 29, 2015 14:01
Show Gist options
  • Save luokebi/eed14b4b027f6729c4e5 to your computer and use it in GitHub Desktop.
Save luokebi/eed14b4b027f6729c4e5 to your computer and use it in GitHub Desktop.
expanding-text-area2
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<textarea id="test"></textarea>
<script type="text/javascript">
function autoExpand(textarea) {
if (textarea.addEventListener) {
textarea.addEventListener('input', fixTextarea, false);
} else if (textarea.attachEvent) {
// IE8 compatibility
textarea.attachEvent('onpropertychange', fixTextarea);
}
function fixTextarea () {
var k = 2,
l = 2
var text = textarea.value;
var m = text.split("\n");
var k = m.length + 1;
for(var i = 0,n=m.length;i < n; i++) {
if (m[i].length > l) {
l = m[i].length;
}
}
textarea.setAttribute('rows', k);
textarea.setAttribute('cols', l);
}
}
autoExpand(document.getElementById('test'));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment