Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy url to clipboard via button click in a vuejs component

I have following component, and I would like to have a button that copies the link_url to the clipboard on click.

I have javascript code that works when selecting an id, however the links do not have an id. Can I accomplish the selection of the a-tag via refs in the component itself, or what would be the best way to get this done.

I was also thinking about generating an a-tag with the this.link_url in the copyURL() dynamically but I guess that would be very dirty.. I am looking for the vuejs way.

<template>
  <li class="list-group-item">
    <a :href="link_url" 
         class="text-dark" 
         target="_blank" 
         rel="noopener noreferrer">{{ link_name }}</a>
    <button @click="copyUrl">copy url from a tag</button>
  </li>      
</template>

<script>
export default {
  props: ["link_url", "link_name"],
  methods: {
    copyURL() {
      var Url = document.getElementById('myid'); /*GET vuejs el reference here (via $ref) but how?*/
      Url.innerHTML = window.location.href;
      console.log(Url.innerHTML)
      Url.select();
      document.execCommand("copy");
    }
  }
}
</script>

<style>
</style>
like image 304
mahatmanich Avatar asked Nov 06 '19 15:11

mahatmanich


People also ask

How do you copy a URL on a button click?

When the button is clicked select the content of #url then copy it to the clipboard.

How do I copy text to clipboard?

To quickly copy selected text or images to the clipboard, use hotkeys Ctrl+C or Ctrl+Insert. These hotkeys work in all Windows programs. Alternatively, you can invoke a pop-up menu by right-clicking on the selected text, and then click Copy.

How to copy url to the clipboard using jQuery?

In the below code, the JavaScript function copyToClipboard is written and specified on the onclick event of the button. The output would be a button, and on the click, you will get the message " URL Copied. ". The following is an example of a Copy URL to the clipboard using jQuery.

How to copy to clipboard with vuejs history?

How to copy to clipboard with VueJS History lesson First of all, I want to dive a bit into the history of copy to clipboard in browsers. The ‘legacy’ API, which still works marvelously, but is technically obsolete, is document.execCommand('copy')whatever is currently selected, will get copied to clipboard. 2 notes about document.execCommand('copy')

Is the ‘legacy’ vuejs API obsolete?

The ‘legacy’ API, which still works marvelously, but is technically obsolete, is document.execCommand('copy')… Open in app Home Notifications Lists Stories Write Alexander Wichmann Carlsen Follow Aug 4, 2021 2 min read Save How to copy to clipboard with VueJS History lesson


1 Answers

You can use navigator object with clipboard in javascript.

Note: navigator.clipboard.writeText is asynchronous.

methods: {
  async copyURL(mytext) {
    try {
      await navigator.clipboard.writeText(mytext);
      alert('Copied');
    } catch($e) {
      alert('Cannot copy');
    }
  }
}
like image 164
Jundell Avatar answered Sep 24 '22 19:09

Jundell