1

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

drai29
  • 141
  • 1
  • 3
  • 19
  • "I am suspecting this has to do with the text being generated using the ajax call." Why would you suspect that? Does `copyToClipboard` expect an element as an argument, or text? –  Jan 05 '18 at 18:33
  • The text being generated is dynamic – drai29 Jan 05 '18 at 18:34
  • 1
    I know that. So? Explain why you think that is relevant. I consider it far more likely that the issue is you're passing an element into `copyToClipboard` when you should be passing that element's content. –  Jan 05 '18 at 18:35
  • can you provide the copyToClipboard function ? – AL-zami Jan 05 '18 at 18:37
  • https://stackoverflow.com/questions/22581345/click-button-copy-to-clipboard-using-jquery – drai29 Jan 05 '18 at 18:39
  • @Amy I am a little stumped on how to pass the elements content – drai29 Jan 05 '18 at 18:43
  • Well it seems to me there's a bug in the `copyToClipboard` function you took from that question. Please paste your current version of that function into your question. –  Jan 05 '18 at 18:55
  • try ctrl+v in notepad to see if something copied to clipboard or not ? code works fine at my end – AL-zami Jan 05 '18 at 19:07
  • @AL-zami it wont copy the content. I have the content being printed on the console but no luck still. – drai29 Jan 05 '18 at 19:11
  • Why did you write an answer saying it was working ("Added this code snippet which worked"), but now you say it isn't working? –  Jan 05 '18 at 19:16
  • I thought it was when I had exactly what I needed printing on the console, I had my function commented when I posted the answer. I jumped ahead of myself @Amy – drai29 Jan 05 '18 at 19:17
  • Okay. Again, please paste **your current version** of that function into the question. –  Jan 05 '18 at 19:18
  • Added, function is from @jfriend00 – drai29 Jan 05 '18 at 19:21

0 Answers0