9

I heard that you can't copy text (in the browser) without using something like Flash; so, is there a way to select text by using an anchor and JavaScript or jQuery.

<p>Text to be copied</p>

<a>Copy Text Above</a>
3
  • Just to clarify: Are you asking how to copy selected text to the clipboard using javascript? Commented Oct 19, 2012 at 20:53
  • No. I'd like it so once the anchor has been clicked, the text (e.g. the text in the <p>) is copied to clipboard. Commented Oct 19, 2012 at 21:01
  • 1
    perhaps something with .text()? Commented Oct 19, 2012 at 21:01

4 Answers 4

31

On newer browsers, you can do this to select and copy. This is a pure Javascript solution.

function copy_text(element) {
    //Before we copy, we are going to select the text.
    var text = document.getElementById(element);
    var selection = window.getSelection();
    var range = document.createRange();
    range.selectNodeContents(text);
    selection.removeAllRanges();
    selection.addRange(range);
    //add to clipboard.
    document.execCommand('copy');
}

This copy command works on all major browsers, Chrome, Firefox (Gecko), Internet Explorer, and Opera, excluding Safari.

Edit: Note for the future - While the preceding still works, there is talk about moving to the Permissions API and using the Clipboard interface, which would look like navigator.clipboard.writeText('text'). This standard is not yet finalized nor supported by many browsers. As security becomes more of a concern, expect something like this in the future.

0
8

I found this jQuery solution:

$(function() {
 $('input').click(function() {
 $(this).focus();
 $(this).select();
 document.execCommand('copy');
 $(this).after("Copied to clipboard");
 });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="copy me!" />

Source

1
  • Finally, thank you. Much harder to find a simple quick jquery hack to do this than you would think. I only need it for local testing so it doesn't need to be the most robust code ever written.
    – Maurice
    Commented May 5, 2020 at 0:46
4

Given the following example html:

<div class="announcementInfoText">
    <p class="copyToClipboard">
        <a id="selectAll">Select All Text</a>
    </p>
    <textarea ID="description" class="announcementTextArea">This is some sample text that I want to be select to copy to the clipboard</textarea>
</div>

you can select the text within the textarea with the following jQuery:

$("#selectAll").click(function () {
    $(this).parents(".announcementInfoText").children("textarea").select();
});

Now that the text "This is some sample text that I want to be select to copy to the clipboard" is selected, you can simply hit Ctrl+C and the text is copied to the clipboard.

3

The simplest solution without running a flash-based plugin would be something like:

$('a').click(function() {
    window.prompt('Press ctrl/cmd+c to copy text', $(this).prev('p').text());
});

http://jsfiddle.net/JFFvG/

Not the answer you're looking for? Browse other questions tagged or ask your own question.