Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize text area to fit all text on load jquery

I understand there has been a lot of discussion on this but I have yet to find a solution to fix my needs. Basically I need to autogrow a text area not when you type but on load. I am pulling in content from a database and dependant on the user's settings an overlay is produced over the text area, but upon doing this the text areas are not scrollable therefore I need to autosize these to show all the text.

I have tried scrollHeight but this is not working great as there are multiple text boxes on the screen

Thanks

like image 994
CR41G14 Avatar asked Oct 26 '12 10:10

CR41G14


People also ask

How to auto resize textarea height in jQuery?

When the content is inserted in the textarea, the height of the textarea field is expanded automatically based on the content. So, the textarea height will be fit to content and scrollbar not visible to the user. At first, include the jQuery library. Add the textarea element which you want to auto resize.

How to set the height of textarea According to its own contents?

You mentioned there are multiple textboxes. This code will set the height of each textarea according to its own contents. $(document).ready( function( ) { $("textarea").each( function( i, el ) { $(el).height( el.scrollHeight ); ​}); });

How to read the width of a text area dynamically?

In the current case it's probably best to read the width dynamically from the cols attribute of textarea. Renaming your variables to something more meaningful than a, b, c would also improve the readability quite a lot.

How to find the relation between text size and text area size?

i lay-man approach can be to define an algorithm for the relation between text size and text area size , depeneding upon this , u can use $('#textAreaID').attr('size',calculatedSize); , a very undelightful but possible solution – Hussain Akhtar Wahid 'Ghouri'


2 Answers

Try this

$("textarea").height( $("textarea")[0].scrollHeight ); 

DEMO


UPDATE

As a hack to make it work in older IE-s just add a really short delay before executing it

window.setTimeout( function() {     $("textarea").height( $("textarea")[0].scrollHeight ); }, 1);​ 

DEMO

UPDATE FOR MULTIPLE TEXTAREAS

$("textarea").each(function(textarea) {     $(this).height( $(this)[0].scrollHeight ); }); 
like image 115
Zoltan Toth Avatar answered Sep 19 '22 15:09

Zoltan Toth


This worked for me; it loops through all "textarea" elements on page Ready, and set their height.

$(function () {     $("textarea").each(function () {         this.style.height = (this.scrollHeight+10)+'px';     }); }); 

You can also combine it with an auto-expand function, to make it fully dynamic while writing as well:

function autoresize(textarea) {     textarea.style.height = '0px';     //Reset height, so that it not only grows but also shrinks     textarea.style.height = (textarea.scrollHeight+10) + 'px';    //Set new height } 

and call that from an "keyup" event or through jQuery:

$('.autosize').keyup(function () {     autoresize(this); }); 

Note how I am adding 10px to the scroll height: here you can adjust the amount of space you would like the bottom of the text area to give the user.

Hope that helps someone. :)

Edit: Changed answer according to @Mariannes comment.

like image 40
Tormod Haugene Avatar answered Sep 17 '22 15:09

Tormod Haugene