How do I copy text to the clipboard within an ajax callback?












0















I need to copy text to the clipboard within an AJAX success callback function. In my code below the text is transferred correctly in to the textarea, but it is not copied to the clipboard.



The code works fine outside of the AJAX callback function. What am I doing wrong?



<textarea id="checktext" name="text"></textarea>


$.ajax({
url: url,
success: function(result) {
document.getElementById("checktext").innerText = result;
// also tried: document.getElementById("checktext").innerHtml = result;
// also tried: $('#checktext').val(result);

document.querySelector("#checktext").select();
document.execCommand("copy");
}
})









share|improve this question

























  • javascript will execute document.getElementById(), document.querySelector() and document.execCommand() at the same time because it is declared withit the same function. You need a callback-function after you filled you textarea with your result.

    – GenericUser
    Nov 15 '18 at 15:31








  • 1





    execCommand() only works when triggered within a user-initiated action, such as a click event. If you're executing this logic when the page loads, it won't work.

    – Rory McCrossan
    Nov 15 '18 at 15:35











  • Here's a working example showing what I mean: jsfiddle.net/rktxzjd2

    – Rory McCrossan
    Nov 15 '18 at 15:36











  • Thanks guys for the answers. Is there any way I can achieve my goal? I need this functionality within the callback. :(

    – bitwikinger
    Nov 15 '18 at 15:38













  • Yes, execute this logic within a user-initiated event handler.

    – Rory McCrossan
    Nov 15 '18 at 15:38
















0















I need to copy text to the clipboard within an AJAX success callback function. In my code below the text is transferred correctly in to the textarea, but it is not copied to the clipboard.



The code works fine outside of the AJAX callback function. What am I doing wrong?



<textarea id="checktext" name="text"></textarea>


$.ajax({
url: url,
success: function(result) {
document.getElementById("checktext").innerText = result;
// also tried: document.getElementById("checktext").innerHtml = result;
// also tried: $('#checktext').val(result);

document.querySelector("#checktext").select();
document.execCommand("copy");
}
})









share|improve this question

























  • javascript will execute document.getElementById(), document.querySelector() and document.execCommand() at the same time because it is declared withit the same function. You need a callback-function after you filled you textarea with your result.

    – GenericUser
    Nov 15 '18 at 15:31








  • 1





    execCommand() only works when triggered within a user-initiated action, such as a click event. If you're executing this logic when the page loads, it won't work.

    – Rory McCrossan
    Nov 15 '18 at 15:35











  • Here's a working example showing what I mean: jsfiddle.net/rktxzjd2

    – Rory McCrossan
    Nov 15 '18 at 15:36











  • Thanks guys for the answers. Is there any way I can achieve my goal? I need this functionality within the callback. :(

    – bitwikinger
    Nov 15 '18 at 15:38













  • Yes, execute this logic within a user-initiated event handler.

    – Rory McCrossan
    Nov 15 '18 at 15:38














0












0








0








I need to copy text to the clipboard within an AJAX success callback function. In my code below the text is transferred correctly in to the textarea, but it is not copied to the clipboard.



The code works fine outside of the AJAX callback function. What am I doing wrong?



<textarea id="checktext" name="text"></textarea>


$.ajax({
url: url,
success: function(result) {
document.getElementById("checktext").innerText = result;
// also tried: document.getElementById("checktext").innerHtml = result;
// also tried: $('#checktext').val(result);

document.querySelector("#checktext").select();
document.execCommand("copy");
}
})









share|improve this question
















I need to copy text to the clipboard within an AJAX success callback function. In my code below the text is transferred correctly in to the textarea, but it is not copied to the clipboard.



The code works fine outside of the AJAX callback function. What am I doing wrong?



<textarea id="checktext" name="text"></textarea>


$.ajax({
url: url,
success: function(result) {
document.getElementById("checktext").innerText = result;
// also tried: document.getElementById("checktext").innerHtml = result;
// also tried: $('#checktext').val(result);

document.querySelector("#checktext").select();
document.execCommand("copy");
}
})






javascript jquery clipboard






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:31









Rory McCrossan

243k29210246




243k29210246










asked Nov 15 '18 at 15:19









bitwikingerbitwikinger

1127




1127













  • javascript will execute document.getElementById(), document.querySelector() and document.execCommand() at the same time because it is declared withit the same function. You need a callback-function after you filled you textarea with your result.

    – GenericUser
    Nov 15 '18 at 15:31








  • 1





    execCommand() only works when triggered within a user-initiated action, such as a click event. If you're executing this logic when the page loads, it won't work.

    – Rory McCrossan
    Nov 15 '18 at 15:35











  • Here's a working example showing what I mean: jsfiddle.net/rktxzjd2

    – Rory McCrossan
    Nov 15 '18 at 15:36











  • Thanks guys for the answers. Is there any way I can achieve my goal? I need this functionality within the callback. :(

    – bitwikinger
    Nov 15 '18 at 15:38













  • Yes, execute this logic within a user-initiated event handler.

    – Rory McCrossan
    Nov 15 '18 at 15:38



















  • javascript will execute document.getElementById(), document.querySelector() and document.execCommand() at the same time because it is declared withit the same function. You need a callback-function after you filled you textarea with your result.

    – GenericUser
    Nov 15 '18 at 15:31








  • 1





    execCommand() only works when triggered within a user-initiated action, such as a click event. If you're executing this logic when the page loads, it won't work.

    – Rory McCrossan
    Nov 15 '18 at 15:35











  • Here's a working example showing what I mean: jsfiddle.net/rktxzjd2

    – Rory McCrossan
    Nov 15 '18 at 15:36











  • Thanks guys for the answers. Is there any way I can achieve my goal? I need this functionality within the callback. :(

    – bitwikinger
    Nov 15 '18 at 15:38













  • Yes, execute this logic within a user-initiated event handler.

    – Rory McCrossan
    Nov 15 '18 at 15:38

















javascript will execute document.getElementById(), document.querySelector() and document.execCommand() at the same time because it is declared withit the same function. You need a callback-function after you filled you textarea with your result.

– GenericUser
Nov 15 '18 at 15:31







javascript will execute document.getElementById(), document.querySelector() and document.execCommand() at the same time because it is declared withit the same function. You need a callback-function after you filled you textarea with your result.

– GenericUser
Nov 15 '18 at 15:31






1




1





execCommand() only works when triggered within a user-initiated action, such as a click event. If you're executing this logic when the page loads, it won't work.

– Rory McCrossan
Nov 15 '18 at 15:35





execCommand() only works when triggered within a user-initiated action, such as a click event. If you're executing this logic when the page loads, it won't work.

– Rory McCrossan
Nov 15 '18 at 15:35













Here's a working example showing what I mean: jsfiddle.net/rktxzjd2

– Rory McCrossan
Nov 15 '18 at 15:36





Here's a working example showing what I mean: jsfiddle.net/rktxzjd2

– Rory McCrossan
Nov 15 '18 at 15:36













Thanks guys for the answers. Is there any way I can achieve my goal? I need this functionality within the callback. :(

– bitwikinger
Nov 15 '18 at 15:38







Thanks guys for the answers. Is there any way I can achieve my goal? I need this functionality within the callback. :(

– bitwikinger
Nov 15 '18 at 15:38















Yes, execute this logic within a user-initiated event handler.

– Rory McCrossan
Nov 15 '18 at 15:38





Yes, execute this logic within a user-initiated event handler.

– Rory McCrossan
Nov 15 '18 at 15:38












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322566%2fhow-do-i-copy-text-to-the-clipboard-within-an-ajax-callback%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322566%2fhow-do-i-copy-text-to-the-clipboard-within-an-ajax-callback%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud