Q:

js copy to clipboard cross browser

function copyToClipboard(text) {
  var input = document.body.appendChild(document.createElement("input"));
  input.value = text;
  input.focus();
  input.select();
  document.execCommand('copy');
  input.parentNode.removeChild(input);
}
1
// Since Async Clipboard API is not supported for all browser!
function copyTextToClipboard(text) {
  var textArea = document.createElement("textarea");
  textArea.value = text
  document.body.appendChild(textArea);
  textArea.focus();
  textArea.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    console.log('Copying text command was ' + msg);
  } catch (err) {
    console.log('Oops, unable to copy');
  }

  document.body.removeChild(textArea);
}
3
$(document).ready(function(){
	$("#ewefmwefmp").click(function(){
    //alert()
      var copyText = document.getElementById("myInput");
      copyText.select();
      copyText.setSelectionRange(0, 99999)
      document.execCommand("copy");
      alert("Copied the text: " + copyText.value);
    
    });
});
2
var copyTextarea = document.getElementById("someTextAreaToCopy");
copyTextarea.select(); //select the text area
document.execCommand("copy"); //copy to clipboard
3
<button id="demo" onclick="copyToClipboard(document.getElementById('demo').innerHTML)">Copy</button>

<script>
  function copyToClipboard(text) {
    window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
  }
</script>
0
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.tooltip {
  position: relative;
  display: inline-block;
}

.tooltip .tooltiptext {
  visibility: hidden;
  width: 140px;
  background-color: #555;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px;
  position: absolute;
  z-index: 1;
  bottom: 150%;
  left: 50%;
  margin-left: -75px;
  opacity: 0;
  transition: opacity 0.3s;
}

.tooltip .tooltiptext::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: #555 transparent transparent transparent;
}

.tooltip:hover .tooltiptext {
  visibility: visible;
  opacity: 1;
}
</style>
</head>
<body>

<p>Click on the button to copy the text from the text field. Try to paste the text (e.g. ctrl+v) afterwards in a different window, to see the effect.</p>

<input type="text" value="Hello World" id="myInput">

<div class="tooltip">
<button onclick="myFunction()" onmouseout="outFunc()">
  <span class="tooltiptext" id="myTooltip">Copy to clipboard</span>
  Copy text
  </button>
</div>

<p>The document.execCommand() method is not supported in IE8 and earlier.</p>

<script>
function myFunction() {
  var copyText = document.getElementById("myInput");
  copyText.select();
  copyText.setSelectionRange(0, 99999);
  document.execCommand("copy");
  
  var tooltip = document.getElementById("myTooltip");
  tooltip.innerHTML = "Copied: " + copyText.value;
}

function outFunc() {
  var tooltip = document.getElementById("myTooltip");
  tooltip.innerHTML = "Copy to clipboard";
}
</script>

</body>
</html>
1
/** 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 || '' ) ) 
	};
    
}
0

New to Communities?

Join the community