question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

executing the copy action programmatically

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
shantanuthattecommented, Dec 16, 2015

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Initiate cut / copy / paste action programmatically
"I would like to add Cut / Copy / Paste items into my context menu I already have available in my JTree control."....
Read more >
how to copy file programmatically just by keypress (ctrl+c)
It introduced the copy & paste action by using the text and file. If you want to do this action inside Windows 8,...
Read more >
How do we ”Call to Existing Action” programmatically (without ...
1) If calling Action from the same script, use the programmatic statement ''RunAction''. 2) If calling Action from another script, make sure the...
Read more >
Cut and copy commands - Chrome Developers
The support for cut and copy commands allows you to programmatically cut and copy selected text to the users clipboard.
Read more >
How to Copy to Clipboard in JavaScript with the Clipboard API
In this guide, we'll take a look at how to programmatically copy ... comes to interfacing automatically with the user and performing actions...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found