<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> text Box Adaptive height according to input </title>
<style type= "Text/css" >
H2 {
Text-align:center;
margin:50px Auto;
}
#textarea {
Display:block;
margin:0 Auto;
Overflow:hidden;
width:550px;
font-size:14px;
height:18px;
line-height:24px;
padding:2px;
border-radius:4px;
min-height:20px;
max-height:70px;
}
TEXTAREA {
outline:0 none;
Border-color:rgba (82, 168, 236, 0.8);
Box-shadow:inset 0 1px 3px rgba (0, 0, 0, 0.1), 0 0 8px Rgba (82, 168, 236, 0.6);
}
</style>
<script>
/** * text box adaptive height based on input content
* @param {htmlelement} input box element
* @param {Number} sets the distance the cursor keeps from the input box (default 0)
* @param {Number} to set the maximum height (optional)
*/
var Autotextarea = function (Elem, Extra, maxheight) {
Extra = Extra | | 0;
var Isfirefox =!! Document.getboxobjectfor | | ' Mozinnerscreenx ' 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.resize = ' None ';
var change = function () {
var scrolltop, height,
padding = 0,
style = Elem.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.documentElement.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 = ' hidden ';
};
Style.height = height + extra + ' px ';
ScrollTop + = parseint (style.height)-elem.currheight;
Document.body.scrollTop = scrolltop;
Document.documentElement.scrollTop = scrolltop;
Elem.currheight = parseint (style.height);
};
};
Addevent (' PropertyChange ', change);
Addevent (' input ', change);
Addevent (' Focus ', change);
Change ();
};
</script>
<body>
<textarea id= "textarea" placeholder= "Reply to Content" ></textarea>
<script>
var text = document.getElementById ("textarea");
Autotextarea (text);//Call
</script>
</body>
HTML5 textarea text box Adaptive height based on input content