I wanna copy some content out of the dataset from an html element.
HTML Code
<p id="web-password-@{{ id }}" data-password="@{{ password }}"
data-state="0">@{{ hidePassword }}</p>
<button class="mdl-button mdl-js-button mdl-button--icon">
<i data-event="copy" data-obj="util" data-target="web-password-@{{ id }}"
class="material-icons clickable-icon">content_copy</i>
</button>
Copy Method
The method is beeing called through the data-event and data-obj attributes.
copy (args) {
let copyText = document.getElementById(args.target).dataset.password;
console.log(copyTest); // output: specific password
document.execCommand("Copy");
}
Like this it do not copy the content to the clipboard. Somebody see an error?
UPDATE
The following code works for the actual textContent of the htmlelement.
But I need to copy the value from data-password attribute
let range = document.createRange();
let selection = window.getSelection();
let node = document.getElementById(args.target);
range.selectNodeContents(node);
selection.removeAllRanges();
selection.addRange(range);
document.execCommand("copy");
Possible solution
So I write the value inside a hidden input field, select it, copy it and delete the temporary hidden input field again.
But it copies nothing.?
let copyValue = document.getElementById(args.target).dataset.password;
document.body.insertAdjacentHTML('beforeend', `<input hidden id="temp-copy" value="${copyValue}">`);
let copyText = document.getElementById('temp-copy');
copyText.select();
document.execCommand("copy");
copyText.remove();