Javascript Copy To Clipboard Example


Share:
Published By: Admin, Published On: Apr 23, 2021, Category: Javascript

Hello Friends,

Now let's see example of how to copy to clipboard in javascript. Here you will learn how to use javascript copy to clipboard. We will use how to copy to clipboard in javascript. This is a short guide on copy to clipboard. Let's get started with how to copy to clipboard in javascript.

Here i will give you many example how you can copy to clipboard in javascript.

Example: 1
<!DOCTYPE html>
<html>
	<head>
	   <title>Javascript - How to copy to clipboard?-MyWebtuts.com</title>
	</head>
	<body>

		<p>Click on the button to copy the text from the text field.</p>

		<input type="text" value="JavaScript" id="test">
		<button onclick="testFunction()">Copy text</button>

		<script>
			function testFunction() {
				var Text = document.getElementById("test");
				Text.select();
				Text.setSelectionRange(0, 99999)
				document.execCommand("copy");
				alert("Copied the text: " + Text.value);
			}
		</script>

	</body>
</html>
Output: 1
Copied the text: JavaScript
Example: 2
<!DOCTYPE html>
<html>
	<head>
	   <title>Javascript - How to copy to clipboard?-MyWebtuts.com</title>
	</head>
	<body>

		<button id="test" onclick="copyToClipboard(document.getElementById('test').innerHTML)">This is what I want to copy</button>

		<script>

			function copyToClipboard(text) {
			    window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
			}

		</script>
		
	</body>
</html>
Output: 2
This is what I want to copy

It will help you....

Share: