copy button code example
Example 1: copy text to clipboard jquery
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).html()).select();
document.execCommand("copy");
$temp.remove();
}
Example 2: how to copy text in the clipboard in js
<html>
<input type="text" value="Hello world"(Can be of your choice) id="myInput"(id is the name of the text, you can change it later)
<button onclick="Hello()">Copy Text</button>
<script>
function Hello() {
var copyText = document.getElementById('myInput')
copyText.select();
document.execCommand('copy')
console.log('Copied Text')
}
</script>
Example 3: copy to clipboard javascript
// 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);
}
Example 4: javascript copy to clipboard
var copyTextarea = document.getElementById("someTextAreaToCopy");
copyTextarea.select(); //select the text area
document.execCommand("copy"); //copy to clipboard
Example 5: how to make a button that paste text in a textbox html
<!DOCTYPE html>
<html>
<head>
<script>
function paste() {
var pasteText = document.querySelector("#text");
pasteText.focus();
document.execCommand("paste");
pasteText.value = pasteText.value + pasteText.value;
}
</script>
</head>
<body>
<input id="text">
<button onclick="paste()">Paste</button>
</body>