I have 3 buttons that copy content from a text area. All three functions are the same, only the variable name changes. How can I combine these into a single function instead of having three separate ones?
<div class="divclip">
<button
type="button"
class="btn btn-light btn-sm"
(click)="copyInputMessage(userinputAscii, 'toogleCopyBtnAscii')"
id="copyBtnOne">
{{ toogleCopyBtnAscii ? "Copy" : "Copied" }}
</button>
</div>
<div class="divclip">
<button
type="button"
class="btn btn-light btn-sm"
(click)="copyInputMessage(userinputHex, 'toogleCopyBtnHex')"
id="copyBtnTwo"
>
{{ toogleCopyBtnHex ? "Copy" : "Copied" }}
</button>
</div>
<div class="divclip">
<button
type="button"
class="btn btn-light btn-sm"
(click)="copyInputMessage(userinputBinary, 'toogleCopyBtnBinary')"
id="copyBtnThree"
>
{{ toogleCopyBtnBinary ? "Copy" : "Copied" }}
</button>
</div>
This is the component.ts code
copyInputMessage(inputElement, toggleVariable) {
this[toggleVariable] = !this[toggleVariable];
inputElement.select();
document.execCommand('copy');
inputElement.setSelectionRange(0, 0);
}