11

I can't get the clipboard.js to work; I think it's a simple "misunderstanding" about how the whole thing works, since I'm not even able to get the most simple example to work properly like in this Fiddle...

My specific problem is getting this to work:

HTML:

<button class="copyButton" 
        id="copyButtonId" 
        data-id="@item.Type" 
        data-clipboard-action="copy" 
        data-clipboard-target="#copy_@item.Type">
</button>  

The div that should be copied is this:

   <div id="copy_@item.Type">
       @item.Type
       Name...: @item.Name
       Address: @item.Address
   </div>`

The JS is:

$(function() {
$(document).on("click", ".copyButton", function() {
    var clipboard = new Clipboard(".copyButton"); 
    clipboard.destroy();
  });
});

I'm getting into the function, but nothing is happening. I tried:

clipboard.copy();

but that just throws me an exception...

I can get the text, that I want to copy

var id= "copy_" + $(this).attr("data-id"); var source = ($("#" + agent).html());

But I should be able only to work it out by using clipboard.js.

I can't get any examples to work, so I would be happy if someone shows me a complete example. I've really tried to understand and I may be overthinking the whole thing and making this more complicated than it is. I don't want any workarounds, as I used that before and think this is a great js-solution... If I could just understand it :)

Every hint into the right direction is appreciated!

devlin carnate
  • 8,309
  • 7
  • 48
  • 82
marS
  • 379
  • 2
  • 3
  • 12

3 Answers3

12

Make sure you add the right library first ;)

<script src="https://cdn.jsdelivr.net/clipboard.js/1.5.3/clipboard.min.js"></script>

Or your local min.js

I've altered your code to this:

<div id="copy">
    @item.Type
    Name...: @item.Name 
    Address: @item.Address
</div>

<button class="copyButton" id="copyButtonId" data-id="@item.Type"
 data-clipboard-action="copy" data-clipboard-target="div#copy">Copy!</button>

and the js:

var clipboard = new Clipboard('.copyButton');
clipboard.on('success', function(e) {
    console.log(e);
});
clipboard.on('error', function(e) {
    console.log(e);
});

With me it copies the div now. Let me know if it doesn't for you.

Victoria S.
  • 549
  • 1
  • 6
  • 20
  • Well,it get copied with your solution, but the thing is: always the same one, thats why I need different ID's. I got a lot of different data loaded, will see if I get it from here – marS Nov 26 '15 at 14:31
  • With me it had issues with the format of the id, so be careful with those. :) – Victoria S. Nov 26 '15 at 14:33
  • it just selects the text it doesnt really copy it. – Brana May 28 '17 at 09:18
  • It selects the text AND copies it. Try pasting somewhere after clicking the button. (https://jsfiddle.net/wm9ce12m/) I made the fiddle again for you. – Victoria S. May 31 '17 at 11:45
  • For those still not working please check the answer here [Illegal constructor in chrome when using clipboard API?](https://stackoverflow.com/questions/30419039/illegal-constructor-in-chrome-when-using-clipboard-api) – Bhautik Jun 29 '21 at 05:17
0

Incase of API data use

import Clipboard from 'clipboard';
''''''
''''''
''''''
 var clipboard = new Clipboard('.class_name_of_the_div');
  clipboard.on('success', function (event) {});
  clipboard.on('error', function (evwnt) {});

data-clipboard-text={data.text}

champion-runner
  • 1,489
  • 1
  • 13
  • 26
-2

Sorry, I haven't got time to read all but for the library https://www.npmjs.com/package/clipboard you can use

import ClipboardJS from 'clipboard'

and then new ClipboardJS('.btn')

or check if this import Clipboard from 'clipboard' works specially for your solution

entymon
  • 62
  • 5