xxxxxxxxxx
function handleCopyTextFromParagraph() {
const body = document.querySelector('body');
const paragraph = document.querySelector('p');
const area = document.createElement('textarea');
body.appendChild(area);
area.value = paragraph.innerText;
area.select();
document.execCommand('copy');
body.removeChild(area);
}
xxxxxxxxxx
function copyToClipboard(text) {
const elem = document.createElement('textarea');
elem.value = text;
document.body.appendChild(elem);
elem.select();
document.execCommand('copy');
document.body.removeChild(elem);
}
xxxxxxxxxx
function copy(value) {
navigator.clipboard.writeText(value);
};
copy("Hello World");
xxxxxxxxxx
#note that this will prompt the user to allow acces to clipboard
navigator.clipboard.readText().then(text => {
console.log('Clipboard content is: ', text);
})
.catch(err => {
console.error('Failed to read clipboard contents: ', err);
});
xxxxxxxxxx
function copy() {
var copyText = document.querySelector("#input");
copyText.select();
document.execCommand("copy");
}
document.querySelector("#copy").addEventListener("click", copy);
xxxxxxxxxx
<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>
xxxxxxxxxx
var el = x;
try {
el.select();
el.setSelectionRange(0, 99999); /* For mobile devices */
document.execCommand("copy");
} catch {
navigator.clipboard.writeText(el.innerText);
}
xxxxxxxxxx
function copyToClipboard(text) {
var copyText = document.createElement("textarea");
copyText.value = text;
document.body.appendChild(copyText);
copyText.select();
document.execCommand("copy");
document.body.removeChild(copyText);
}
// Usage example:
var textToCopy = "Hello, world!";
copyToClipboard(textToCopy);
xxxxxxxxxx
navigator.clipboard
.writeText("Text")
.then(() => console.log("Copied to clipboard"))
.catch((err) => console.log(err))