user7461846
user7461846

Reputation:

calling a function vs coding directly

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?

Upvotes: 0

Views: 24

Answers (1)

triunenature
triunenature

Reputation: 671

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);
});

Upvotes: 1

Related Questions