How to Make Textarea Auto Resize using 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Convert Object to String in Javascript?
- Javascript Convert Array into Comma Separated String Example
- How to Get Last Element from Array using JQuery?
- How to Check Array Is Empty Or Null In Javascript?
- How to Generate Random String in Javascript?
- How to Set Maxlength for Textarea in Javascript?
- How to Get First Element of Array in Javascript?
- How to Get Last Element of Array in Javascript?
- How to Check Undefined, Empty and Null in JQuery?