0

I have a reference to a textarea

<*wrapper-component (submit)="tx1.value ? submit(tx1.value) : tx1.setClass('required')??">
<textarea #tx1> <textarea>
</*wrapper-component>

which I pass the value of into my submit function, is it possible to set classes for my tx1 element from the HTML itself instead of creating a ViewChild property and accessing elRef.nativeElement.setClass with typeScript?

Kiran Mistry
  • 2,614
  • 3
  • 12
  • 28
Steve Chacko
  • 95
  • 11

1 Answers1

1

I'd normally suggest using [ngClass] binding for setting CSS selectors dynamically. However, if you insist on using a template ref variable, you could use the setAttribute() method to set attributes, in your case 'class'.

<*wrapper-component (submit)="tx1.value ? submit(tx1.value) : tx1.setAttribute('class', 'required')">
  <textarea #tx1> <textarea>
</*wrapper-component>
ruth
  • 29,535
  • 4
  • 30
  • 57
  • 1
    Thanks for the answer! I used ```tx1.classList.add('required')``` because I wanted to retain the classes already existing on the textarea element. – Steve Chacko Nov 17 '21 at 05:16