How to Make Textarea Auto Resize using Javascript?

By Hardik Savani September 6, 2020 Category : Javascript

This simple article demonstrates of how to make textarea auto resize using javascript. i would like to share with you auto resize textarea to fit content javascript. I’m going to show you about javascript textarea grow automatically. you will learn javascript auto expand textarea. Follow bellow tutorial step of javascript autosize textarea height.

If you need to set textarea auto height based on content using javascript then i will help you how to auto resize height of textarea in javascript. i will give you two examples of auto adjust textarea height using javascript.

So, let's see bellow preview and examples:

Preview:

Example 1:

<!DOCTYPE html>

<html>

<head>

<title>Javascript Auto-resize Textarea as User Types Example</title>

</head>

<body>

<h1>Javascript Auto-resize Textarea as User Types Example - ItSolutionStuff.com</h1>

<strong>Body:</strong>

<br/>

<textarea id="body" style="width: 400px;"></textarea>

<script type="text/javascript">

textarea = document.querySelector("#body");

textarea.addEventListener('input', autoResize, false);

function autoResize() {

this.style.height = 'auto';

this.style.height = this.scrollHeight + 'px';

}

</script>

</body>

</html>

Example 2:

<!DOCTYPE html>

<html>

<head>

<title>Textarea Auto Height Based on Content Jquery</title>

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

</head>

<body>

<h1>Textarea Auto Height Based on Content Jquery Example - ItSolutionStuff.com</h1>

<strong>Body:</strong>

<br/>

<textarea id="body" style="width: 400px;"></textarea>

<script type="text/javascript">

$('#body').on('input', function () {

this.style.height = 'auto';

this.style.height = (this.scrollHeight) + 'px';

});

</script>

</body>

</html>

I hope it can help you...

Tags :
Shares