How to create auto-resize textarea using JavaScript/jQuery ? - GeeksforGeeks

03 Aug, 2021
Create a textarea and the task is to automatically resize it when we type or paste the content in it. It can be achieved by using JavaScript and jQuery. 
Method 1: Using JavaScript: To change the height, a new function is created which changes the style property of the textarea. The height of the textarea is first set to auto. This value makes the browser set the height of an element automatically. This will make the text scrollable as the textarea height is lesser than the text. Immediately on the next line, the height is again set equal to that of the scrollHeight.
The scrollHeight property is used to return the entire height of an element including padding in pixels. This will make the textarea height equal to the height of the whole text area, effectively resizing the textarea to fit the text.
The ‘input’ event is fired whenever the value of an input or textarea changes. This event can be detected using the addEventListener() method. The callback function of this method is set to the new function created above. This will trigger the textarea to resize whenever any text input is detected, therefore automatically resizing according to the text being typed or pasted.
Example:  
HTML
<!DOCTYPE html><html>  <head>    <title>        How to create auto-resize textarea        using JavaScript/jQuery?    </title>      <style>        #autoresizing {            display: block;            overflow: hidden;            resize: none;        }    </style></head>  <body>    <h1 style="color: green">        GeeksforGeeks    </h1>          <b>        Creating a textarea with        auto-resize in JavaScript    </b>            <p>        The textarea below will resize        automatically accounting for        cut, paste and typing text.    </p>            <textarea id="autoresizing">        Try cutting, pasting        or typing here    </textarea>          <script type="text/javascript">        textarea = document.querySelector("#autoresizing");        textarea.addEventListener('input', autoResize, false);              function autoResize() {            this.style.height = 'auto';            this.style.height = this.scrollHeight + 'px';        }    </script></body>  </html>
Output: 
Before writing any text:
After writing the text:
Method 2: Using jQuery: It is similar to the above used method. The on() method in jQuery is used to attach an event handler to any element. The textarea is first selected and this method is used to apply an event handler on the selected element. A new function is declared in the callback which changes the style property of the textarea. The height of the textarea is first set to ‘auto’ and then immediately on the next line, the height is again set equal to that of the scrollHeight.
This will make the textarea’s height equal to the height of the whole text area, effectively resizing the textarea to fit the text. This function would be executed whenever a change in input is detected and the textarea would appear to resize automatically.
Example:  
HTML
<!DOCTYPE html><html>  <head>    <title>        How to create auto-resize textarea        using JavaScript/jQuery?    </title>          <script src="https://code.jquery.com/jquery-3.4.1.min.js">    </script>          <style>        #autoresizing {            display: block;            overflow: hidden;            resize: none;        }    </style></head>  <body>    <h1 style="color: green">        GeeksforGeeks    </h1>          <b>        Creating a textarea with        auto-resize in JavaScript    </b>            <p>        The textarea below will resize        automatically accounting for cut,        paste and typing text.    </p>            <textarea id="autoresizing">        Try cutting, pasting or typing here    </textarea>          <script type="text/javascript">        $('#autoresizing').on('input', function () {            this.style.height = 'auto';                          this.style.height =                     (this.scrollHeight) + 'px';        });    </script></body>  </html>
Output: 
Before writing any text:
After writing the text:
 
jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.
vikassnwl
JavaScript-Misc
jQuery-Misc
Picked
JavaScript
JQuery
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
JQuery | Set the value of an input text field
How to change selected value of a drop-down list using jQuery?
Form validation using jQuery
How to change the background color after clicking the button in JavaScript ?
How to Dynamically Add/Remove Table Rows using jQuery ?