is it possible to copy text programmatically with this library or is a HTML element and user input (click) required?
I'm looking for something like:
var c = new Clipboard();
c.copyText('some text');
maybe the ClipboardAction class could be made public?
Due to security restrictions imposed by browser vendors, user interaction (click) is required in order copy/cut to work.
Hi,
I know of the security restrictions by the browser. And my event is triggered by user action.
What I have is a typeahead box. When a user selects a suggestion (by using arrow keys and pressing enter, or by clicking on the suggestion), I want to trigger the copy.
$('#menu-input').bind('typeahead:selected', function(obj, datum, name) {
var url = "https://example.com/"+datum.url;
$("#typeahead-url").html(url);
$("#copy-clipboard").data('clipboard-text', url);
copy_url();
});
The current copy_url() is working, but would like to use clipboard.js. Is there a way to trigger now?
function copy_url () {
var urlNode = document.getElementById("typeahead-url");
var s = window.getSelection();
if(s.rangeCount > 0) s.removeAllRanges();
var range = document.createRange();
range.selectNode(urlNode);
s.addRange(range);
try {
// Now that we've selected the url text, execute the copy command
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Url copied: ' + msg);
} catch(err) {
console.log('Oops, unable to copy');
}
}
Also, reassigning the data-clipboard-text doesn't seem to update what is copied.
can you explain about the ClipboardAction?
Most helpful comment
Hi,
I know of the security restrictions by the browser. And my event is triggered by user action.
What I have is a typeahead box. When a user selects a suggestion (by using arrow keys and pressing enter, or by clicking on the suggestion), I want to trigger the copy.
The current
copy_url()is working, but would like to use clipboard.js. Is there a way to trigger now?Also, reassigning the
data-clipboard-textdoesn't seem to update what is copied.