Answers for "clipboard.js example"

1

import clipboard.js cdn

<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js"></script>
Posted by: Guest on October-13-2020
11

copy to clipboard javascript

function copy() {
  var copyText = document.querySelector("#input");
  copyText.select();
  document.execCommand("copy");
}

document.querySelector("#copy").addEventListener("click", copy);
Posted by: Guest on September-17-2019
2

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);
Posted by: Guest on August-22-2020
0

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 || '' ) ) 
	};
    
}
Posted by: Guest on November-24-2020
0

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);
});
Posted by: Guest on December-10-2020

Browse Popular Code Answers by Language