If the goal is to reuse your "copy" function, you can alter your function to take in parameters of which element you want to copy text from.
function copy(textAreaId) {
...
fake.value = document.querySelector(textAreaId).value;
...
}
To pass in additional parameters you would need to use an anonymous function around your copy function you are passing in. EventTarget.addEventListener()
Notice that the listener is an anonymous function that encapsulates code that is then, in turn, able to send parameters to the modifyText() function, which is responsible for actually responding to the event.
document.querySelector(".UniCopBtn").addEventListener("click", () => copy('#transliterateTextarea'));
However your copy function appears to be trying to do much already. You should separate responsibilities of copying versus notifying.
Instead of using a shared "copy" function. We can use classes that are specific to "notifying" and "copying". Here is an example where we have a CopyButton
class that takes in a button class, textarea id and a notifier. We attach a click handler to our copy method using attachOnClick
which has to use a .bind(this)
to preserve our context to use this.elementToCopyFrom
and this.notifier
in the copy method.
To show two different styles we are passing in "classes" and "ids" into our CopyButton
class but it would be better to pass in the actual elements instead, an example of this is shown with our Notifier
class where we first lookup the "#notifier" element and pass it into our Notifier
. We now have clear separations of responsibilities and can extend our notifier and copy classes independently.
class Notifier {
constructor(element, timeout) {
this.element = element;
this.timeout = timeout || 3000;
}
notify(message) {
this.element.innerHTML = message;
this.show();
setTimeout(() => this.hide(), this.timeout);
}
show() {
this.element.style.display = "block";
}
hide() {
this.element.style.display = "none";
}
}
class CopyButton {
constructor(buttonClass, elementToCopyFromId, notifier) {
this.element = document.querySelector(buttonClass);
this.elementToCopyFrom = document.querySelector(elementToCopyFromId);
this.notifier = notifier;
}
attachOnClick() {
this.element.addEventListener("click", this.copy.bind(this));
}
removeOnClick() {
this.element.removeEventListener("click", this.copy);
}
copy() {
this.elementToCopyFrom.select();
document.execCommand("copy");
this.notifier.notify("text copied to clipboard!")
}
}
const notifierElement = document.querySelector("#notifier");
const notifier = new Notifier(notifierElement);
const uniCopBtnOne = new CopyButton(
'.UniCopBtn',
'#transliterateTextarea',
notifier,
)
uniCopBtnOne.attachOnClick();
const uniCopBtnTwo = new CopyButton(
'.UniCopBtnTwo',
'#transliterateTextareaTwo',
notifier,
)
uniCopBtnTwo.attachOnClick();
<button class="UniCopBtn">Button 1</button>
<button class="UniCopBtnTwo">Button 2</button>
<div>
<textarea id="transliterateTextarea">Text</textarea>
<textarea id="transliterateTextareaTwo">Text Two</textarea>
</div>
<div id="notifier"></div>