Home » Javascript » How to add a meta tag to clipboard when copying programmatically with javascript?

How to add a meta tag to clipboard when copying programmatically with javascript?

Posted by: admin August 19, 2018 Leave a comment

Questions:

I am trying to programmatically insert a table into clipboard to be pasted into google spreadsheets.

Here is what I have done:

$('.copy').click(function() {
	var copyContainer = $('<div>'); //a hidden container to copy from
	copyContainer.append('<meta name="generator" content="Sheets"/>');
		
	copyContainer.css('position', 'absolute').css('z-index', -999).css('opacity', 0);
	$('body').prepend(copyContainer);
	copyContainer.attr('contenteditable', true);
	
    // Let's grab a table from html to make example simpler
	copyContainer.append($('table').clone());
	copyContainer.select();
		
	copyContainer.on('focus', function() { 
		document.execCommand('selectAll',false,null) 
	});
	copyContainer.focus();
		
	document.execCommand('copy');
	copyContainer.remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="copy">Copy!</button>
<table><tr><td data-sheets-note="test">123</td></tr></table>
Answers: