How to copy href atrribute using clipboard.js

after clicking the button .btn

, I would like to copy the attribute href

to the clipboard.

here is the html source (clipboard.js recognizes any type as a button)

<a class="btn" href="#oops"><h1>Button</h1></a>

      

and below is the script:

<script type="text/javascript">
        new Clipboard(".btn", {
          text: function(trigger) {
            return $(trigger).getAttribute("href");
          }
        });
</script>   

      

But I cannot get it to work successfully. If someone could help me for a second, I would be very grateful.

+3


source to share


1 answer


This works for me



new Clipboard('.btn', {
    text: function (trigger) {
        return trigger.getAttribute('href');
    }
});
      

<script src="//cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.4.0/clipboard.min.js"></script>
<a class="btn" href="#oops"><h1>Button</h1></a>
      

Run codeHide result


+4


source







All Articles