2

Jquery/HTML5 Text copy after click element? I have text, and inside is span with name "copy" and i want to copy value of .copy span after click.

This is my text:

<span class="copy">Donec laoreet interdum</span> lectus, vel pharetra   ligula mollis sit amet. Quisque in nibh id felis scelerisque.

this is my jquery code:

$(function () {
    $('.copy').click(function(){
        // copy action, ?
    });
});

after click i want copy "Donec laoreet interdum" to clipboard. please help, best regards!

user3501587
  • 425
  • 5
  • 16

4 Answers4

3

JavaScript copy to clipboard isn't available because of security which also mean that jQuery isn't able to copy the text to clipboard. You can do it with flash.

http://www.paulund.co.uk/jquery-copy-clipboard

Adam
  • 126
  • 9
  • 1
    take a look of github repo, there is also some button to copy repo address. And it is a flash element. U can not doing in pure JS, what's means that libs or plugins can't exist also. – Adam Oct 30 '14 at 14:20
1

I wouldn't use Adobe Flash anymore — browser support is declining. There is a new HTML5-JS-only "Clipboard API and events" (Working Draft 21 April 2015 at the time of this writing) which also let's you copy/paste into your OS clipboard.

Frank Lämmer
  • 2,165
  • 19
  • 26
0

You can get the text content like this

    $('.copy').text()

To copy the fetched text to clipboard you can use a liv such as ZeroClipboard But this will only work when the user actually clicks on the 'trigger' button. A java script simulated 'click' on the trigger will not work

shakirthow
  • 1,356
  • 14
  • 15
0

Well it is not recommended by Ecmascript specification to directly copy into user clipboard. One easy way to achieve the similar kind of result by the code below

function copyToClipboard(text) {
    window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
}

IE has a method for accessing clipboard. Code below works only for IE

<script type="text/javascript">
function select_all(obj) {
    var text_val=eval(obj);
    text_val.focus();
    text_val.select();
    if (!document.all) return; // IE only
    r = text_val.createTextRange();
    r.execCommand('copy');
}
</script>
<input value="http://www.sajithmr.com"
       onclick="select_all(this)" 
       name="url" type="text" />

How ever cross browser access to clipboard can be gained using flash. There is an elegant plugin for copping into clipboard: zeroclipboard.org/

Tousif Osman
  • 287
  • 2
  • 12