Copy Div content to Clipboard

<div id="myDiv">This is the content to get copied to the text area and then to clipboard</div>

<script type="text/javascript">
function copy2cb()
{
// If it's FireFox, then sets the

// "signed.applets.codebase_principal_support" to TRUE
if (navigator.userAgent.indexOf("Firefox")!=-1)
user_pref("signed.applets.codebase_principal_support", true);

var ta= document.getElementById ("myhidta");
var d= document.getElementById ("code_collective");
// Transfer the text from the DIV to the hidden textarea
ta.innerText= d.innerText;
// create the text range
var t= ta.createTextRange ();
// do this if you wanna remove the format of the text within
t.execCommand('RemoveFormat');
// Then execute the copy command
t.execCommand('Copy');
}
</script>

<textarea style="display:none;" id="myhidta"></textarea>
<input type="button" value="Copy" onClick="copy2cb();"/>

0 Comments

You must be logged in to post a comment.