0

Copy to clipboard with old browsers support

Function JavaScript 1 revisions 139 3 months ago 3 months ago
function copyToClipboard(text) {
	const clipboard = document.createElement('textarea');

	clipboard.style.position = 'fixed';
	clipboard.style.top = 0;
	clipboard.style.left = 0;
	clipboard.style.width = '2em';
	clipboard.style.height = '2em';
	clipboard.style.padding = 0;
	clipboard.style.border = 'none';
	clipboard.style.outline = 'none';
	clipboard.style.boxShadow = 'none';
	clipboard.style.background = 'transparent';
	clipboard.value = text;

	document.body.appendChild(clipboard);

	clipboard.focus();
	clipboard.select();

	const successful = document.execCommand('copy');
	console.log(successful);

	document.body.removeChild(clipboard);
}