red-devil red-devil - 5 months ago 30
AngularJS Question

Resizable Textarea that resizes in the up direction (No Jquery)

I need to make Html Textarea input to auto-resize in the UP direction with increase in the content. Check out this fiddle which auto-resizes Textarea in the opposite direction.

<!DOCTYPE html>
<html>
<head>
<title>autoresizing textarea</title>
<style type="text/css">
textarea {
border: 0 none white;
overflow: hidden;
padding: 0;
outline: none;
background-color: #D0D0D0;
resize: none;
}
</style>
<script type="text/javascript">
var observe;
if (window.attachEvent) {
observe = function (element, event, handler) {
element.attachEvent('on'+event, handler);
};
}
else {
observe = function (element, event, handler) {
element.addEventListener(event, handler, false);
};
}
function init () {
var text = document.getElementById('text');
function resize () {
text.style.height = 'auto';
text.style.height = text.scrollHeight+'px';
}
/* 0-timeout to get the already changed text */
function delayedResize () {
window.setTimeout(resize, 0);
}
observe(text, 'change', resize);
observe(text, 'cut', delayedResize);
observe(text, 'paste', delayedResize);
observe(text, 'drop', delayedResize);
observe(text, 'keydown', delayedResize);

text.focus();
text.select();
resize();
}
</script>
</head>
<body onload="init();">
<textarea rows="1" style="height:1em;" id="text"></textarea>
</body>
</html>


Any suggestions to get this done.

PS: Can't use Jquery. Angularjs can be used if required.

Answer

I am slightly puzzled as to what you are trying to do. I am assuming there will be some space for the textarea to expand into above it? If that is the case, it is easy. No scripting required...

put the text area inside a div and assign it the following css (for example):

height: 200px;
vertical-align: bottom;
display: table-cell;

https://jsfiddle.net/WM6Gq/695/