0

I'm able to increase textarea height as textrows add up (based on this question/answer: Adjust TD height depending on textarea rows).

Now, how do I reduce rows as I remove text? The answer above only adds rows.

Community
  • 1
  • 1
October Eleven
  • 142
  • 2
  • 13

1 Answers1

0

If you want it more stable, you can count the linebreaks. So you can be sure that it set exactly the right number of rows:

$('.expand').on('keydown', function(e) {
    if(13==(e.keyCode ? e.keyCode : e.which)) {
        var rows = $(this).attr('rows');
        var rowsNew = parseInt(rows) + 1;
        $(this).attr('rows', rowsNew);
    }
});
$('.expand').on('keyup', function (e) {
    var lines = $('.expand').val().match(/\n/g);
    var need = lines ? lines.length + 1 : 1;
    var itis = $('.expand').attr("rows");
    if(itis!=need) {
        $('.expand').attr("rows", need);
    }
});

Demo here: http://jsfiddle.net/efhYe/

But if you have jQuery already, you can also use this plugin: http://www.jacklmoore.com/autosize/

Varon
  • 3,736
  • 21
  • 21