admin管理员组文章数量:1334133
i have a div
with id
copy
and a button.i want when that button is clicked i copy the div
content into the clipboard
function myFunction() {
var copyText = document.getElementById("copy");
copyText.innerHTML=html;
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
}
that is what i have tried so far but does not seem to work. please help me. how can i go about this??
i have a div
with id
copy
and a button.i want when that button is clicked i copy the div
content into the clipboard
function myFunction() {
var copyText = document.getElementById("copy");
copyText.innerHTML=html;
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
}
that is what i have tried so far but does not seem to work. please help me. how can i go about this??
Share Improve this question asked Mar 18, 2020 at 18:56 steveyoutsteveyout 1793 silver badges13 bronze badges 1- Does this answer your question? How do I copy to the clipboard in JavaScript? – hagello Commented Mar 18, 2020 at 19:49
3 Answers
Reset to default 3I used this method and it worked:
function CopyToClipboard(id){
var r = document.createRange();
r.selectNode(document.getElementById(id));
window.getSelection().removeAllRanges();
window.getSelection().addRange(r);
try {
document.execCommand('copy');
window.getSelection().removeAllRanges();
console.log('Successfully copy text: hello world ' + r);
} catch (err) {
console.log('Unable to copy!');
}
}
<div id="div_id">Hello World From div Content</div>
<button type="button" onclick="CopyToClipboard('div_id')">Copy text</button>
i got this code from here.
//In HTML
<button type="button" id="copy">COPY</button>
<div id="output"></div>
//In Javascript
let copyBtn = document.getElementById('copy');//Get The Div
copyBtn.addEventListener('click', () => {
const text = document.getElementById('output').innerText
navigator.clipboard.writeText(text);
})
I have only been able to copy text from <input>
and <textarea>
. So my strategy was to copy the text from the <div>
to a invisible <textarea>
.
But I couldn't get the copy.value to display in the alert properly (it cancelled the clipboard copy for some reason). So I just used the value of the copyText
function myFunction() {
// get the div contents
let copyText = document.getElementById("copy").innerHTML;
// get the textarea element
var copy = document.getElementById("copyTextarea");
// move the content from the div to the textarea
copy.value = copyText;
// select the content inside the textarea
copy.select();
copy.setSelectionRange(0, 99999);
// copy to the clipboard
document.execCommand("copy");
// alert
alert("Copied the text: " + copyText);
}
You'd need to create your <div>
and your <textarea>
:
<div id="copy" onclick="myFunction()">Simple Test</div>
<textarea style="display: none;" id="copyTextarea"></textarea>
本文标签: javascripthow to copy div content into clipboardStack Overflow
版权声明:本文标题:javascript - how to copy div content into clipboard - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742358980a2459981.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论