0

I have this code for textarea autoheight

   function h(el) {
        $(el).height(el.scrollHeight);
    }

$('textarea').each(function () {
    h(this); //  works
    //$(this).height($(this).scrollHeight);  //  doesn't work
});

$('textarea').on('input', function () {
    h(this);  // works
    //$(this).height($(this).scrollHeight);  //  dosen't work
});

If I replace h(this) with commented lines - they doesn't work. What is the reason?

1 Answers1

1

In your group that doesn't work, you are converting this to jQuery both on .height and .scrollHeight, but in your function h, you only convert the el to jQuery on height

$('textarea').each(function () {
    $(this).height(this.scrollHeight);
});
triunenature
  • 651
  • 2
  • 7
  • 23