Creating a textarea with auto-resize Creating a textarea with auto-resize javascript javascript

Creating a textarea with auto-resize


A COMPLETE YET SIMPLE SOLUTION

Updated 2020-05-14(Improved browser support for mobiles and tablets)

The following code will work:

  • On key input.
  • With pasted text (right click & ctrl+v).
  • With cut text (right click & ctrl+x).
  • With pre-loaded text.
  • With all textarea's (multiline textbox's) site wide.
  • With Firefox (v31-67 tested).
  • With Chrome (v37-74 tested).
  • With IE (v9-v11 tested).
  • With Edge (v14-v18 tested).
  • With IOS Safari.
  • With Android Browser.
  • With JavaScript strict mode.
  • Is w3c validated.
  • And is streamlined and efficient.

OPTION 1 (With jQuery)

This option requires jQuery and has been tested and is working with 1.7.2 - 3.6

Simple (Add this jquery code to your master script file and forget about it.)

$("textarea").each(function () {  this.setAttribute("style", "height:" + (this.scrollHeight) + "px;overflow-y:hidden;");}).on("input", function () {  this.style.height = "auto";  this.style.height = (this.scrollHeight) + "px";});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><textarea placeholder="Type, paste, cut text here...">PRELOADED TEXT.This javascript should now add better support for IOS browsers and Android browsers.</textarea><textarea placeholder="Type, paste, cut text here..."></textarea>