0

I use this function to autosize textarea:

$('textarea').each(function () {
    this.setAttribute('style', 'height:' + (this.scrollHeight) + 'px;overflow-y:hidden;');
    }).on('input', function () {
        this.style.height = 'auto';
        this.style.height = (this.scrollHeight) + 'px';
    });

I have this workflow: user goes to page A, fills textarea and saves it, then he closes page A and visit it again, textarea resizes only on input, but I need to resize it every time as user visits this page.

Vadim Novozhilov
  • 855
  • 9
  • 20
  • Take a look at thi solution http://stackoverflow.com/a/13085420/2990234 – Anfuca Nov 19 '16 at 10:48
  • If you close and reopen the page, the javascript variable for the previous scrollHeight will be lost out of scope. If you dont have a way of saving the value to a backend, I'd recommend taking a look at browser localStorage for a get/set on the textarea height. Also, why not just set `this.style.height= 'auto'; this.style.height = (this.scrollHeight) + 'px';` outside of the `.on('input'` part of the code, in the `.each` part? – Xenyal Nov 19 '16 at 10:51
  • `.trigger('input')`?! – A. Wolff Nov 19 '16 at 10:53
  • Thanks for replies, guys. Unfortunately, I have no way to save such data to db. I tried to set values in `.each` part, didn't work. `.trigger('input')` doesn't work as well, tried it too. – Vadim Novozhilov Nov 19 '16 at 11:11

0 Answers0