Javascript text box textarea height scales proportionally as content grows _ form Effects

Source: Internet
Author: User
A function used in the project some time ago, using Javascript to control the textarea height of the text box to scale proportionally as the content grows. Today, I have spent some time changing the implementation method to summarize it. Directly run the Code:
Solution 1:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.1 // EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <HTML> <HEAD> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "> <TITLE> fengzhi» text box textarea highly adaptive growth/scaling </TITLE> <style> textarea {height: 100px; width: 300px ;} </style> </HEAD> <BODY> <! -- In the following code, onpropertychange: IE supported; oninput: FireFox supported; added to be compatible with IE and FF; --> <textarea id = "txtContent" rows = "1" onpropertychange = "ResizeTextarea ()" oninput = "ResizeTextarea ()" onkeyup = "ResizeTextarea () "> Qingfeng production http://jb51.net </textarea> <script type =" text/javascript "> // minimum height var minHeight = 100; // maximum height, if the value is exceeded, the scroll bar var maxHeight = 300 is displayed. function ResizeTextarea () {var t = document. getElementById ('txtcontent '); H = t. scrollHeight; h = h> minHeight? H: minHeight; h = h> maxHeight? MaxHeight: h; t. style. height = h + "px";} script </BODY> </HTML>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Solution 1 in various browsers, the text box is no problem with the adaptive growth of content; however, the contraction of the deleted content is different, and IE and Opera are normal, firefox, Chrome, and Safari will not contract. The reason is that the scrollHeight value is equal to the text box height when the text box content height is smaller than the text box height, rather than the text box content height.
Solution 2:

The Code is as follows:







Fengyunzhi> text box textarea highly adaptive growth/scaling


The Textarea height increases with the Content Self-adaptive and has no scroll bar.
Qingfeng Production
Http://jb51.net </textarea>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.