Clipboard.js: executing the copy action programmatically

Created on 14 Nov 2015  路  5Comments  路  Source: zenorocha/clipboard.js

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');
question

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.

$('#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.

All 5 comments

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?

Was this page helpful?
0 / 5 - 0 ratings