-3

I'm working on a textarea that has no [rows] attribute. On first, the height should be 35px.

----------


Text Here

----------

Then, if the user break a new line [Shift + Enter]. the textarea should be like this.

------------------

Text Here

Another Line

------------------

But, on the next new line. the textarea should be scrollable.

Thanks in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Riku
  • 652
  • 1
  • 9
  • 24
  • 1
    Possible duplicate of [Creating a textarea with auto-resize](https://stackoverflow.com/questions/454202/creating-a-textarea-with-auto-resize) – prasanth Sep 20 '17 at 11:09

3 Answers3

1

You should add following code:

style: max-height: 35px; overflow: scroll;
  • Hi Michal, I already did that. but on first the height should be 35 and should adjustable up to 75px. and then scrollable. but when you enter Shift+enter the height didn't adjust – Riku Sep 20 '17 at 11:14
0

You can check scrollHeight of your textarea at every keyup event and then set height accordingly.

  $('textarea').keyup(function(){
    $(this)[0].style.height = ($(this)[0].scrollHeight)+"px";
});

And also you need to set overflow hidden.

textarea {
    resize: none;
    overflow: hidden;
    min-height: 20px; 
}

Fiddle

Amit Kumar
  • 5,888
  • 11
  • 47
  • 85
0
var text    = $(this).val();
            var newline = text.split('\n');
            if(newline.length === 2) {
                $el.removeClass('scroll');
                $el.attr({
                    style : 'height : 60px'
                });
            }
            if(newline.length === 3) {
                $el.removeClass('scroll');
                $el.attr({
                    style : 'height : 75px'
                });
            }
            if(newline.length > 3) {
                $el.addClass('scroll');
            }

            if(newline.length === 0 || newline.length === 1) {
                $el.removeClass('scroll');
                $el.prop('style',false);
            }

I don't know if it is a good idea.

Riku
  • 652
  • 1
  • 9
  • 24