xxxxxxxxxx
//ths will copy image if image source is other then your own website.
function copyImage (imageid) {
const img = document.getElementById(imageid);
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
fetch(img.src, { mode: 'cors' })
.then((response) => response.blob())
.then((blob) => {
const url = URL.createObjectURL(blob);
const img = new Image();
img.crossOrigin = 'anonymous';
img.onload = () => {
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0);
canvas.toBlob((blob) => {
const item = new ClipboardItem({ 'image/png': blob });
navigator.clipboard.write([item]).then(() => {
hideShowToast('Image copied to clipboard');
}).catch((error) => {
hideShowToast('Could not copy image to clipboard', 1);
});
}, 'image/png');
};
img.src = url;
})
.catch((error) => {
hideShowToast('Could not copy image to clipboard', 1);
});
}
xxxxxxxxxx
function copy(value) {
navigator.clipboard.writeText(value);
};
copy("Hello World");
xxxxxxxxxx
var el = x;
try {
el.select();
el.setSelectionRange(0, 99999); /* For mobile devices */
document.execCommand("copy");
} catch {
navigator.clipboard.writeText(el.innerText);
}
xxxxxxxxxx
navigator.clipboard
.writeText("Text")
.then(() => console.log("Copied to clipboard"))
.catch((err) => console.log(err))
xxxxxxxxxx
function textToClipboard (text) {
var dummy = document.createElement("textarea");
document.body.appendChild(dummy);
dummy.value = text;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
}
xxxxxxxxxx
try {
navigator.clipboard.write([
new ClipboardItem({
'image/png': pngImageBlob
})
]);
} catch (error) {
console.error(error);
}
xxxxxxxxxx
var copyTextarea = document.getElementById("someTextAreaToCopy");
copyTextarea.select(); //select the text area
document.execCommand("copy"); //copy to clipboard
xxxxxxxxxx
navigator.clipboard.writeText("Hello, World!");
xxxxxxxxxx
$(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);
});
});