Skip to content Skip to sidebar Skip to footer

Copy Text From A Hidden Control Using JavaScript

I want to be able to place text in a textarea, and copy it to the clipboard, without displaying the textarea. To copy the text to the clipboard, I create a button with onclick = 'd

Solution 1:

Use style ='display:block; width:0; height:0; opacity: 0;' instead of visibility

<head>
<script type='text/javascript'>
function c2cb () {
document.getElementById("txtInvoice").select();
document.execCommand('copy');
}
</script>
</head>
<body ><form id="frmAdminConsole" name="frmAdminConsole" METHOD="POST">

<textarea id='txtInvoice' cols='80' style='display:block; width:0; height:0; opacity: 0;'>



        46


JOHN SMITH
GAEL SCOIL NA BFHAL
34 A IVEAGH CRESENT
BELFAST
BT12 6AW



Bubble Ball Football [2017-02-03 09:00]     20  190.00
Nerf Wars [2017-02-05 10:00]    14  190.00
TeamTrek [2017-02-06 12:00]     20  0.00</textarea>

<input type="button" value="Copy!" onclick="c2cb()">

Solution 2:

I don't have enough "reputations" to comment, so this is entered as an answer. I found that Chrome will not copy the content of an element with width: 0 or font-size: 0 (works in FireFox). I have an absolutely positioned element with opacity: 0, height: 0, placed outside the visible area. I can copy that element just fine, but if I add either width: 0 or font-size: 0, Chrome clears the clipboard instead of copying the selection.


Post a Comment for "Copy Text From A Hidden Control Using JavaScript"