At the moment, I have a string that was generated using an AJAX Call. I am trying to create a copy button that will copy that text which was generated. At the moment, not able to copy the text within the div.
document.getElementById("copyButton").addEventListener("click", function () {
copyToClipboard(document.getElementById("htmlCode"));
});
function copyToClipboard(elem) {
var targetId = "_hiddenCopyText_";
var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
var origSelectionStart, origSelectionEnd;
if (isInput) {
target = elem;
origSelectionStart = elem.selectionStart;
origSelectionEnd = elem.selectionEnd;
} else {
target = document.getElementById(targetId);
if (!target) {
var target = document.createElement("textarea");
target.style.position = "absolute";
target.style.left = "-9999px";
target.style.top = "0";
target.id = targetId;
document.body.appendChild(target);
}
target.textContent = elem.textContent;
}
var currentFocus = document.activeElement;
target.focus();
target.setSelectionRange(0, target.value.length);
var succeed;
try {
succeed = document.execCommand("copy");
} catch (e) {
succeed = false;
}
// restore original focus
if (currentFocus && typeof currentFocus.focus === "function") {
currentFocus.focus();
}
if (isInput) {
// restore prior selection
elem.setSelectionRange(origSelectionStart, origSelectionEnd);
} else {
// clear temporary content
target.textContent = "";
}
return succeed;
}
I have a function copytoClipboard that copies the text, I have this working elsewhere. I am suspecting this has to do with the text being generated using the ajax call.
Thanks