import clipboard.js cdn
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js"></script>
import clipboard.js cdn
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js"></script>
copy to clipboard javascript
function copy() {
var copyText = document.querySelector("#input");
copyText.select();
document.execCommand("copy");
}
document.querySelector("#copy").addEventListener("click", copy);
js copy string to clipboard
const el = document.createElement('textarea');
el.value = str; //str is your string to copy
document.body.appendChild(el);
el.select();
document.execCommand('copy'); // Copy command
document.body.removeChild(el);
js copy to clipboard cross browser
/** copy text to the clipboard */
function copy2clipboard( text, callback /* optional */ ) {
// use modern clipboard API
if ( navigator.clipboard ) {
navigator.clipboard.writeText( text )
.then( function(){
// if a callback is provided, call it
callback && callback();
}).catch( function( err ){
errorMessage( err );
});
}
// use old document.execCommand('copy')
else {
// create a temporary textArea containing the text
var textArea = document.createElement( 'textarea' );
textArea.setAttribute( 'style', 'width:1px;border:0;opacity:0;' );
document.body.appendChild( textArea );
textArea.value = text;
// select the textArea
textArea.select();
try {
// copy from textArea
var isCopied = document.execCommand('copy');
// if copy was successful, and a callback is provided, call it. if copy failed, display error message
isCopied ? ( callback && callback() ) : errorMessage();
}
catch( err ) {
errorMessage( err );
}
// remove temporary textArea
document.body.removeChild( textArea );
}
/** display error message */
function errorMessage( err ) {
alert( 'Copy to clipboard failed ' + ( err || '' ) )
};
}
data-clipboard-target
var clipboard = new ClipboardJS('.btn');
clipboard.on('success', function(e) {
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
e.clearSelection();
});
clipboard.on('error', function(e) {
console.error('Action:', e.action);
console.error('Trigger:', e.trigger);
});
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us