3

I am trying to copy text from a HTML textarea, however I only found a solution with an Input tag like this:

<input type="text" value="User input Text to copy" #userinput>
<button (click)="copyInputMessage(userinput)" value="click to copy">Copy from Textbox</button>

Function:

copyInputMessage(inputElement){
  inputElement.select();
  document.execCommand('copy');
  inputElement.setSelectionRange(0, 0);
}

When I want to replace the input tag with a textarea tag it doesn’t work anymore. Is there an similar easy solution with a textarea?

Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
pattplatt
  • 63
  • 8

2 Answers2

0

Try like this:

Working Demo

<textarea type="text" #userinput></textarea>
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
0
<textarea cols="30" rows="4" [(ngModel)] = "userinput"></textarea>




<button (click)="copyInputMessage()" value="click to copy">Copy from Textbox</button>

Create a Variable in Ts file As String And Using Property binding bind this with Text area value

 userinput: string;


copyInputMessage(this.userinput){
  inputElement.select();
  document.execCommand('copy');
  inputElement.setSelectionRange(0, 0);
}

if You want to use template reference then

<textarea cols="30" rows="4" #userinput></textarea>

in Ts file

@ViewChild('userinput') nameInputRef: ElementRef;
harkesh kumar
  • 833
  • 2
  • 13
  • 35