Last active
August 29, 2015 14:01
-
-
Save luokebi/eed14b4b027f6729c4e5 to your computer and use it in GitHub Desktop.
expanding-text-area2
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
<!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