0

I need autosize (elastic) for <textarea>. I need to use <textarea>, cannot use <ion-textarea> for other issues.

I was following: How to autoresize the ion-input field vertically while i am typing & https://stackblitz.com/edit/expandable-input

<ion-textarea> is working:

@Directive({ selector: 'ion-textarea[autosize]' })

But, Not for <textarea>

@Directive({ selector: 'textarea[autosize]' })

Here is My Stackblitz (autosize not working): https://stackblitz.com/edit/expandable-input-htjncw

Console error: "ERROR TypeError: Cannot read property 'style' of undefined ..."

Code:

adjust():void {
    const textArea = this.element.nativeElement.getElementsByTagName('textarea')[0];
    textArea.style.overflow = 'hidden';
    textArea.style.height = 'auto';
    textArea.style.height = textArea.scrollHeight + 'px';
  }
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Amin9
  • 23
  • 6

1 Answers1

0

If you are adding the directive to textarea instead of ion-textarea, you can get access to the html element directly from nativeElement. You wont be accessing through getElementsByTagName

adjust():void {
    const textArea = this.element.nativeElement;
    textArea.style.overflow = 'hidden';
    textArea.style.height = 'auto';
    textArea.style.height = textArea.scrollHeight + 'px';
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103