Skip to content

Instantly share code, notes, and snippets.

@saravanakumarputta
Created July 27, 2020 10:47
Show Gist options
  • Save saravanakumarputta/6981fbdb22b603c69e5abd316b399821 to your computer and use it in GitHub Desktop.
Save saravanakumarputta/6981fbdb22b603c69e5abd316b399821 to your computer and use it in GitHub Desktop.
Copy Text 2 ClipBoard
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Copy Text 2 Clipboard</title>
</head>
<body>
<div>
<input type="text" id="text" placeholder="Enter text" />
<button onClick="copyTextToClipBoard()">Copy To ClipBoard</button>
</div>
<script>
function copyTextToClipBoard() {
//Input Element with id "text"
let textToBeCopied = document.getElementById('text');
//Select the content in the input element
textToBeCopied.select();
textToBeCopied.setSelectionRange(0, 99999);
//copy the text inside the input element to clipboard
document.execCommand('copy');
alert('Text copied to Clipboard');
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment