2

how to make ClipboardJS can copy text along with it's hyperlink? I already tried using data-link but no luck,

clipboard.on('success', function(e) {
    var aff_link = $(this).attr('data-link'); 
});

this is what I already tried to make, and the html is <span id="clipboard" data-clipboard-text="this is a text" data-link="#">copy</span>

2 Answers2

0

You can try this with hidden input value.

var clipboard = new Clipboard('.btn-copy', {
  text: function() {
    return document.querySelector('input[type=hidden]').value;
  }
});
clipboard.on('success', function(e) {
  alert("Copied!");
  e.clearSelection();
});
$("#input-url").val(location.href);
.wrapper {
  width: 100%;
  height: 100%;
  text-align: center;
  margin-top: 10px;
}

.btn-copy {
  background-color: #38AFDD;
  border: transparent;
  border-bottom: 2px solid #0086B7;
  border-radius: 2px;
  padding: 10px;
  min-width: 100px;
  color: #fff;
}

.btn-copy:hover,
.btn-copy:focus {
  background-color: #48A1C1;
  border-bottom: 2px solid #38AFDD;
  /*transition cross browser*/
  transition: all .3s ease-in;
  -webkit-transition: all .3s ease-in;
  -moz-transition: all .3s ease-in;
  -o-transition: all .3s ease-in;
}
<script src="https://cdn.jsdelivr.net/clipboard.js/1.5.10/clipboard.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
  <h3>Click button below to copy current url to clipboard with hidden input</h3>
  <input type="hidden" id="input-url" value="www.google.com">
  <button class="btn-copy">Copy</button>
</div>
Sumit Patel
  • 4,530
  • 1
  • 10
  • 28
  • thank you for your answer, but this is not what I want. what I want is something like this. when you click the button, the text will be copied, and then when you paste it on gmail, it'll showing `facebook` with a hyperlink of `https://facebook.com` – Khrisna Gunanasurya Aug 19 '19 at 04:51
0
Easy Code ;)

<!-- The text field -->
    <input type="text" value="Hello World" id="myInput">

    <!-- The button used to copy the text -->
    <button onclick="myFunction()">Copy text</button>


    function myFunction() {
      /* Get the text field */
      var copyText = document.getElementById("myInput");

      /* Select the text field */
      copyText.select();

      /* Copy the text inside the text field */
      document.execCommand("copy");

      /* Alert the copied text */
      alert("Copied the text: " + copyText.value);
    }
Lars Gross
  • 102
  • 6
  • thanks for your answer! but I think your code only copying the text, not the hyperlink as well, I need a way to copy rich text format, because I want the hyperlink to be copied to the clipboard – Khrisna Gunanasurya Aug 20 '19 at 08:21