1

I need increase the text area height based on content. if user type the content it automatically increase. and if delete the content it need reduce the height.

Creative Enemy
  • 399
  • 4
  • 22

3 Answers3

2

Yes i got the answer, its work fine.

<textarea onkeyup="textAreaAdjust(this)" placeholder="Add a new answer" rows="1"></textarea>


 <script type="text/javascript"> 
                     function textAreaAdjust(o) {  
                     o.style.height = "1px";     o.style.height = (25+o.scrollHeight)+"px"; }
  </script>
Creative Enemy
  • 399
  • 4
  • 22
1

Try This:

<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>
<body onload="init();">
<textarea rows="1" style="height:1em;" id="text"></textarea>
</body>
</html>

Here is Demo

Reference Creating a textarea with auto-resize

Community
  • 1
  • 1
Ganesh Salunkhe
  • 596
  • 1
  • 4
  • 18
1

you can use jquery autogrow pluing

Code :

 $('textarea').autogrow({onInitialize: true});

Note : you need to include autogrow plugin file in to page

Nishit Maheta
  • 6,021
  • 3
  • 17
  • 32