JavaScript function copies text from input field to clipboard when you click button. It looks as following:
<script type="text/javascript">
(function () {
'use strict';
// click events
document.body.addEventListener('click', copy, true);
// event handler
function copy(e) {
// find target element
var
t = e.target,
c = t.dataset.copytarget,
inp = (c ? document.querySelector(c) : null);
// is element selectable?
if (inp && inp.select) {
// select text
inp.select();
try {
// copy text
document.execCommand('copy');
inp.blur();
}
catch (err) {
alert('please press Ctrl/Cmd+C to copy');
}
}
}
})();
</script>
Usage:
<button id="CopyTextBtn" autofocus
type="submit"
class="uui-button lime-green"
data-copytarget="#ClientsURL"
ng-click="closeThisDialog('Cancel')">
Copy
</button>
I’ve tried this:
appModule.directive('data-copytarget', function () {
return {
scope: {},
link: function (scope, element)
{
// click events
document.body.addEventListener('click', copy, true);
// event handler
function copy(e) {
// find target element
var
t = e.target,
c = t.dataset.copytarget,
inp = (c ? document.querySelector(c) : null);
// is element selectable?
if (inp && inp.select) {
// select text
inp.select();
try {
// copy text
document.execCommand('copy');
inp.blur();
}
catch (err) {
alert('please press Ctrl/Cmd+C to copy');
}
}
}
}
};
});
But, it doesn’t work.