I’m trying to do a simple auto-expanding textarea. This is my code:
textarea.onkeyup = function () {
textarea.style.height = textarea.clientHeight + 'px';
}
But the textarea just keeps growing indefinitely as you type…
I know there is Dojo and a jQuery plugin for this, but would rather not have to use them. I looked at their implementation, and was initially using scrollHeight but that did the same thing.
You can start answering and play with the textarea for your answer to play with.
Reset the height before Using
scrollHeightto expand/shrink the textarea correctly.Math.min()can be used to set a limit on the textarea’s height.Code:
Fiddle: http://jsfiddle.net/gjqWy/155
Note: The
inputevent is not supported by IE8 and earlier. Usekeydownorkeyupwithonpasteand/oroncutif you want to support this ancient browser as well.