0

I've seen all kind of jquery plugins for autogrowing a textarea. But I hope there may be an easy way to let a textarea element autogrow in pure javascript (anno 2015).

I'm only targeting modern browsers. But I also appreciate answers that target older browsers.

Kasper
  • 12,594
  • 12
  • 41
  • 63

1 Answers1

1

This seems to work. Not tested it that well though.

<textarea></textarea>
<script>
    var textarea = document.querySelector('textarea');
    textarea.addEventListener('input', function() {
        textarea.style.height = 'auto';
        textarea.style.height = textarea.scrollHeight + 'px';
    });
</script>
Kasper
  • 12,594
  • 12
  • 41
  • 63