Copy span text using Clipboard.js

Rose

I'm using clipboard.js and need to copy the text in a span by clicking a button. Is there a way to do this?

HTML:

<span id="spanId">text here</span>
<input type="button" class="buttonClass" value="Copy" data-clipboard-target="#spanId" />
gaetanoM

A solution can be:

// create a new instance of Clipboard plugin for the button element
// using the class selector: .buttonClass
var clipboard =  new Clipboard('.buttonClass');


// when text is copied into clipboard use it
clipboard.on('success', function(e) {
  $('#log').text('Text copied into clipboard is: <' + e.text + '>');
  e.clearSelection();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.10/clipboard.min.js"></script>

<span id="spanId">text here</span>
<input type="button" class="buttonClass" value="Copy" data-clipboard-target="#spanId"/>
<p id="log"></p>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy text js to clipboard

From Dev

how to copy selected text to the clipboard using javascript

From Dev

Using execCommand (Javascript) to copy hidden text to clipboard

From Dev

How to copy text to the clipboard when using Wayland?

From Dev

Copy table row using clipboard.js

From Dev

Copy table row using clipboard.js

From Dev

Copy text to Clipboard in MFC

From Dev

Copy to clipboard as plain text

From Dev

copy text in clipboard

From Dev

Autoselect text with clipboard copy

From Dev

Copy text and html to clipboard

From Dev

Copy to clipboard with click JS

From Dev

clipboard.js Copy text in side closest <pre>

From Dev

Is it possible to embed JS into an <a> element to copy static text to the user's clipboard?

From Java

Copy a given text of paragraph tag to clipboard using JavaScript

From Dev

How can I copy text to the clipboard using AppleScript

From Dev

How to copy text including special characters to clipboard using C and WinAPI?

From Dev

How to copy text from command line to clipboard without using the mouse?

From Dev

How can I copy text to the clipboard using AppleScript

From Dev

How to copy text including special characters to clipboard using C and WinAPI?

From Dev

Copy URL from browser using clipboard.js

From Dev

How to copy using clipboard.js with dynamic content and triggers

From Dev

Copy text from CardView to Clipboard

From Dev

Copy text with same class to clipboard

From Dev

Text does not copy to clipboard android

From Dev

Dynamically copy text to clipboard in a table

From Dev

Copy text to clipboard on page ready

From Dev

Text in clipboard with protractor js

From Dev

Copy text to clipboard now that execCommand("copy") is obsolete

Related Related

HotTag

Archive