red-devil
red-devil

Reputation: 1103

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.

Upvotes: 2

Views: 7272

Answers (2)

tomijovanoski
tomijovanoski

Reputation: 661

Actually, the direction that is auto-resizing depends on the position of the element.

I managed to achieve that by simple css to the textarea element:

position: absolute;
bottom: 0;
top: auto;
max-height: 150px;
overflow: auto;

The max-height is limiting the box to grow more than 150px and overflow:auto makes it scrollable.

https://jsfiddle.net/tomijovanoski/q0w68p6p/

Upvotes: 3

tpie
tpie

Reputation: 6221

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/

Upvotes: 5

Related Questions