Text area textarea box adaptive height based on input content

Source: Internet
Author: User

On the Internet, I found that the method is simple to implement highly adaptive text box, so I encapsulated this function to be fully compatible with IE6 + and modern browsers! The specific function code is as follows:

/*** Adaptive height of the text box based on the input content * @ authortang bin * @ version0.3 * @ see http://www.phpernote.com/article-9-32.html * @ Param {HTMLElement} input box element * @ param {Number} sets the distance between the cursor and the input box (20 by default) * @ param {Number} sets the maximum height (optional) */var autoTextarea = function (elem, extra, maxHeight) {extra = extra | 20; var isFirefox = !! Document. getBoxObjectFor | 'initnerscreenx' in window, isOpera = !! Window. opera &&!! Window. opera. toString (). indexOf ('Opera '), addEvent = function (type, callback) {elem. addEventListener? Elem. addEventListener (type, callback, false): elem. attachEvent ('on' + type, callback) ;}, getStyle = elem. currentStyle? Function (name) {var val = elem. currentStyle [name]; if (name = 'height' & val. search (/px/I )! = 1) {var rect = elem. getBoundingClientRect (); return rect. bottom-rect. top-parseFloat (getStyle ('paddingtop ')-parseFloat (getStyle ('paddingbottom') + 'px' ;}; return val ;}: function (name) {return getComputedStyle (elem, null) [name] ;}, minHeight = parseFloat (getStyle ('height'); elem. style. maxHeight = elem. style. resize = 'none'; var change = function () {var scrollTop, height, padding = 0, style = ele M. style; if (elem. _ length = elem. value. length) return; elem. _ length = elem. value. length; if (! IsFirefox &&! IsOpera) {padding = parseInt (getStyle ('paddingtop ') + parseInt (getStyle ('paddingbottom');}; scrollTop = document. body. scrollTop | document.doc umentElement. scrollTop; elem. style. height = minHeight + 'px '; if (elem. scrollHeight> minHeight) {if (maxHeight & elem. scrollHeight> maxHeight) {height = maxHeight-padding; style. overflowY = 'auto';} else {height = elem. scrollHeight-padding; style. overflowY = 'den den ';}; style. height = height + extra + 'px '; scrollTop + = parseInt (style. height)-elem. currHeight; document. body. scrollTop = scrollTop;document.doc umentElement. scrollTop = scrollTop; elem. currHeight = parseInt (style. height) ;};}; addEvent ('propertychang', change); addEvent ('input', change); addEvent ('focal ', change); change ();};

The call method is as follows (textarea_id is the id value of textarea ):

autoTextarea(document.getElementById('textarea_id'));

Another simple implementation method:

<Html> Articles you may be interested in

 
  • Javascript restricts that only numbers (including decimal places) can be entered in the text box)
  • Display text box effects of line numbers, compatible with browsers such as ie and Firefox
  • Completely solves the adaptive width problem of the Button in IE6 and 7.
  • Jump to the corresponding Email homepage Based on the Email entered by the user
  • A solution that does not work when textarea submits content
  • Code used to test the password strength of a user input using Javascript
  • Js address bar special effects (display the size of all images with links on the page and view the height of the current browser)
  • How to Use zlib to compress output content to speed up web page opening

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.