Copy to clipboard button

Add a button to a card to copy its contents to the clipboard

Often you want to create a button that would copy certain content into a clipboard.


  • Have a vector of multiple cards
  • Each card to have a small copy button
  • If the user clicks that button, only the content of the associated card will be copied to clipboard

Example contributed by Jan Siml.

@app begin
    @in my_texts = [Dict(:id => id, :content => content) for (id, content) in zip(1:3, ["abc", "def", "ghi"])]
    @in copied_index = 0

function ui()
    h3("Some Text"),
    card(class = "q-my-md", @for("(item, index) in my_texts"), key = :index, [
        btn(icon = "content_paste", "", :flat, :dense, class = "absolute-right", @click("""
          copied_index = index
          this.\$q.notify('Copied ' + index + '!')
    htmldiv("Copied Index: {{ copied_index }}")

@methods begin
    copyToClipboard: function(str) {
        const el = document.createElement('textarea');  // Create a <textarea> element
        el.value = str;                                 // Set its value to the string that you want copied
        el.setAttribute('readonly', '');                // Make it readonly to be tamper-proof = 'absolute'; = '-9999px';                      // Move outside the screen to make it invisible
        document.body.appendChild(el);                  // Append the <textarea> element to the HTML document
        const selected = this.my_text;                                    // Select the <textarea> content
        document.execCommand('copy');                   // Copy - only works as a result of a user action (e.g. click events)
        document.body.removeChild(el);                  // Remove the <textarea> element