.copy()


.copy()Returns: jQuery

Description: Copies element content to clipboard.

  • version added: v0.1.1.copy()

    • This method does not accept any arguments.

This function will copy the contents of an element.

Consider the following example:

1
2
3
4
5
6
7
<div>
<p class="copy">Some text to copy</p>
</div>

<script>
$( ".copy" ).copy();
</script>

The text "Some text to copy" will be copied to the clipboard. This process will work as if you selected the text manually and then copied by right-clicking or by using keyboard shortcuts. This will not invote a copying event listener, however.

The .copy() function will work differently when copying a table:

1
2
3
4
5
6
7
8
9
10
11
12
<table>
<tbody>
<tr>
<td>Line 1</td>
<td>Line 2</td>
</tr>
</tbody>
</table>

<script>
$( "table" ).copy();
</script>

In this case, the .copy() function will copy the outer HTML of the table. The entire element is copied so it will retain all its table-loke properties when pasted into a spreadsheed editor, like Excel.