Skip Navigation

[Resolved] Autosize textarea in form

This support ticket is created 4 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by RensV5812 4 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1595897

Hi guys,

I've added a form that has a textarea and I would like it to resize depending on how many rows of text are typed. For this I added the following JS:

// Applied globally on all textareas with the "autoExpand" class
(function($) {
  $(document)
      .one('focus.autoExpand', 'textarea.autoExpand', function(){
          var savedValue = this.value;
          this.value = '';
          this.baseScrollHeight = this.scrollHeight;
          this.value = savedValue;
      })
      .on('input.autoExpand', 'textarea.autoExpand', function(){
          var minRows = this.getAttribute('data-min-rows')|0, rows;
          this.rows = minRows;
          rows = Math.ceil((this.scrollHeight - this.baseScrollHeight) / 16);
          this.rows = minRows + rows;
      })
})(jQuery);

It works on a <textarea class='autoExpand'> tag, but not on the generic cred field. I think it's because the rows attribute is hardcoded in there (it's always 5 even though I add 'rows="5"').

You can see a working example here: hidden link. For now I'm just using some CSS to set the height, but I would like to see it auto-resizing.

Do you know a way to make it work?

Rens

#1596131

Hi, unfortunately this type of customization of Forms fields falls outside the scope of support we provide here in the forums. There is no JavaScript API for forms, so I don't have much I can offer here. You might try attaching your event listeners to a different element, as Forms may prevent events from bubbling up that far. Beyond that, you're on your own here for customization outside of the scope of support.

#1601223

My issue is resolved now. Thank you!