admin管理员组

文章数量:1415095

I have created a textarea that outputs custom text. I want to have it scale to fit the text if there is too much, and not have to scroll (unless the textarea is a certain height). I also do not want to be able to manually scale the textarea. How would I go about doing this? Thank you in advance.

I have created a textarea that outputs custom text. I want to have it scale to fit the text if there is too much, and not have to scroll (unless the textarea is a certain height). I also do not want to be able to manually scale the textarea. How would I go about doing this? Thank you in advance.

Share Improve this question asked Apr 8, 2015 at 6:59 Dr. OwningDr. Owning 1733 silver badges14 bronze badges 2
  • 1 HTML <textarea> Tag – ozil Commented Apr 8, 2015 at 7:04
  • do you also want textarea of fixed width? – nehal gala Commented Apr 8, 2015 at 7:10
Add a ment  | 

2 Answers 2

Reset to default 5

You might want to take a look at this. Seems to be a solution for you problem. There is a jsfiddle in the accepted answer that shows the result.

To take out the most important function see below. It sets the textareas height equal to its scrollHeight.

function resize () {
    var text = document.getElementById('text');
    text.style.height = 'auto';
    text.style.height = text.scrollHeight+'px';
}

You have do this via CSS

textarea
{ 
   overflow : auto;
}

本文标签: javascriptMake textarea scale vertically to fit textStack Overflow