JavaScript Auto Expand Textarea

by Shobhit Sharma

HTML

<textarea id="txtarea"n class="myclass" spellcheck="false" placeholder="Statusku...">bbbbbb</textarea>

CSS

body {
  padding:50px;  
}

textarea {
  margin:0px 0px;
  padding:5px;
  height:16px;
  line-height:16px;
  width:96%;
  display:block;
  margin:0px auto;    
}

JavaScript

function expandTextarea(id) {
    var $element = $('.myclass').get(0);  
    
    $element.addEventListener('keyup', function() {
        this.style.overflow = 'hidden';
        this.style.height = 0;
        this.style.height = this.scrollHeight + 'px';
    }, false);
}

expandTextarea('txtarea');