Creating auto-resize text area using JavaScript

JavascriptWeb DevelopmentObject Oriented Programming

Following is the code for creating auto resize text area using JavaScript −

Example

 Live Demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
</style>
</head>
<body>
<h1>Creating auto-resize text area</h1>
<textarea class="resizeArea"></textarea>
<script>
   let BtnEle = document.querySelector(".Btn");
   let addrEle = document.querySelectorAll(".addr");
   let resizeEle = document.querySelector(".resizeArea");
   resizeEle.addEventListener("input", resizeTextArea, false);
   function resizeTextArea() {
      this.style.height = "auto";
      this.style.height = this.scrollHeight + "px";
   }
</script>
</body>
</html>

Output

On pasting text in the text area, the area will expand as follows −

raja
Published on 16-Jul-2020 16:58:59
Advertisements