Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 13, 2023 01:40
Show Gist options
  • Save code-boxx/a4dad267237792e400a4399f15642c2e to your computer and use it in GitHub Desktop.
Save code-boxx/a4dad267237792e400a4399f15642c2e to your computer and use it in GitHub Desktop.
Javascript Copy Cut Paste

JAVASCRIPT CLIPBOARD - COPY CUT PASTE

https://code-boxx.com/copy-cut-paste-vanilla-javascript/

IMAGE

demo

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Javascript Copy</title>
</head>
<body>
<script>
// (A) COPY TEXT TO CLIPBOARD
function jscopyA () {
// (A1) GET TEXT FROM TEXT FIELD
var txt = document.getElementById("demoA").value;
// (A2) PUT TEXT INTO CLIPBOARD
navigator.clipboard.writeText(txt)
// (A3) OPTIONAL - DO THIS AFTER COPY
.then(() => alert("Copied"));
}
</script>
<!-- (B) HTML -->
<input type="text" id="demoA" value="Hello World!" readonly>
<input type="button" value="Copy Text Field" onclick="jscopyA()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Javascript Copy</title>
</head>
<body>
<script>
// (A) COPY TEXT TO CLIPBOARD
function jscopyB () {
// (A1) GET TEXT FROM TEXT FIELD
var txt = document.getElementById("demoB").innerText;
// (A2) PUT TEXT INTO CLIPBOARD
navigator.clipboard.writeText(txt)
// (A3) OPTIONAL - DO THIS AFTER COPY
.then(() => alert("Copied"));
}
</script>
<!-- (B) HTML -->
<p id="demoB">This is a <strong>random</strong> string.</p>
<input type="button" value="Copy Text Content" onclick="jscopyB()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Javascript Copy</title>
</head>
<body>
<script>
// (A) COPY IMAGE TO CLIPBOARD
function jscopyC () {
// (A1) FETCH IMAGE
fetch ("demo.png")
.then (res => res.blob())
.then (blob => {
/*
console.log(blob);
console.log(blob.size);
console.log(blob.type);
*/
// (A2) IMAGE BLOB TO CLIPBOARD
navigator.clipboard.write([new ClipboardItem({[blob.type]: blob})])
// (A3) OPTIONAL - DO THIS AFTER COPY
.then(() => alert("Copied"));
});
}
</script>
<!-- (B) HTML -->
<input type="button" value="Copy" onclick="jscopyC()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Javascript Cut</title>
</head>
<body>
<script>
// (A) CUT TEXT TO CLIPBOARD
function jscut () {
// (A1) GET TEXT FROM TEXT FIELD
var txt = document.getElementById("demoD").value;
// (A2) PUT TEXT INTO CLIPBOARD
navigator.clipboard.writeText(txt)
// (A3) EMPTY TEXT FIELD AFTER COPY
.then(() => document.getElementById("demoD").value = "");
}
</script>
<!-- (B) HTML -->
<input type="text" id="demoD" value="Hello World!" readonly>
<input type="button" value="Cut" onclick="jscut()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Javascript Paste</title>
</head>
<body>
<script>
// (A) PASTE FROM CLIPBOARD
function jspaste () {
// (A1) READ TEXT FROM CLIPBOARD - PERMISSION REQUIRED
navigator.clipboard.readText()
// (A2) PUT CLIPBOARD INTO TEXT FIELD
.then(txt => document.getElementById("demoE").value = txt)
// (A3) OPTIONAL - CANNOT ACCESS CLIPBOARD
.catch(err => alert("Please allow clipboard access permission"));
}
</script>
<!-- (B) HTML -->
<input type="text" id="demoE">
<input type="button" value="Paste" onclick="jspaste()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Javascript Paste</title>
</head>
<body>
<script>
// (A) COPY
function oldcopy () {
document.getElementById("demoF").select();
document.execCommand("copy");
alert("Text copied");
}
// (B) CUT
function oldcut () {
document.getElementById("demoF").select();
document.execCommand("cut");
alert("Text copied");
}
</script>
<!-- (B) HTML -->
<input type="text" id="demoF" value="Foo Bar">
<input type="button" value="Copy" onclick="oldcopy()">
<input type="button" value="Cut" onclick="oldcut()">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment