whiteBear22
whiteBear22

Reputation: 407

Angular 9 Copy to clipboard with string concatenation

I'm struggling with a problem. I want to create a copy to clipboard functionality. This is my code:

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

that works great, but I have a problem. I would like to concatenate string from this input with another string value, before it invokes an 'copy' exec command. It should concatenate previously set string to the begin of this input value. Is it possible to make it this way?

Thanks!

Upvotes: 0

Views: 251

Answers (1)

Rafał Gołubowicz
Rafał Gołubowicz

Reputation: 660

There are some options. The first is to use external library, like clipboard.js or something like that.

The second option is to dynamic create an element, which will be filled programically, and you'll select this input, then execute command.

const input = document.createElement('input');
input.style.visibility = 'hidden';
input.value = yourPrefix + yourValueFromInput; // (hope you have 2-way binding, so you'll have values in variables)
input.select();
document.execCommand('copy');
input.parentNode.removeChild(input);

Upvotes: 1

Related Questions