Copy to clipboard with jQuery (from a html tag ) [duplicate]










0
















This question already has an answer here:



  • How to copy text from a div to clipboard

    5 answers



I am currently struggling with copying to clipboard via jQuery.



The code I have works if I use via on-click="Myfunction", but since I need to use it in several places, I would like to apply the function to all elements with a class. This is what I have tried.






$(".btn-clipboard").click(function(e)
copyToClipboard($(this));
);

function copyToClipboard(element)
var $temp = $("<textarea>");
$("body").append($temp);
$temp.val($(element).siblings("code").text()).select();
document.execCommand("copy");
$temp.remove();
;

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bd-clipboard">
<button class="btn-clipboard" title="" data-original-title="Copy to clipboard" >Copy</button>
</div>
<figure>
<pre>
<code class="language-html" data-lang="html"><br>
<span> code example that should be copied</span>
</code>
</pre>
</figure>












share|improve this question















marked as duplicate by Mohammad, Heretic Monkey, Foo, cнŝdk, Graham Nov 14 '18 at 3:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • It i snot clear what exactly you want

    – brk
    Nov 13 '18 at 14:20











  • Not sure if it will make any difference, put you're passing in $(this) to a parameter element and then wrapping it up again as $(element)

    – freefaller
    Nov 13 '18 at 14:21















0
















This question already has an answer here:



  • How to copy text from a div to clipboard

    5 answers



I am currently struggling with copying to clipboard via jQuery.



The code I have works if I use via on-click="Myfunction", but since I need to use it in several places, I would like to apply the function to all elements with a class. This is what I have tried.






$(".btn-clipboard").click(function(e)
copyToClipboard($(this));
);

function copyToClipboard(element)
var $temp = $("<textarea>");
$("body").append($temp);
$temp.val($(element).siblings("code").text()).select();
document.execCommand("copy");
$temp.remove();
;

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bd-clipboard">
<button class="btn-clipboard" title="" data-original-title="Copy to clipboard" >Copy</button>
</div>
<figure>
<pre>
<code class="language-html" data-lang="html"><br>
<span> code example that should be copied</span>
</code>
</pre>
</figure>












share|improve this question















marked as duplicate by Mohammad, Heretic Monkey, Foo, cнŝdk, Graham Nov 14 '18 at 3:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • It i snot clear what exactly you want

    – brk
    Nov 13 '18 at 14:20











  • Not sure if it will make any difference, put you're passing in $(this) to a parameter element and then wrapping it up again as $(element)

    – freefaller
    Nov 13 '18 at 14:21













0












0








0









This question already has an answer here:



  • How to copy text from a div to clipboard

    5 answers



I am currently struggling with copying to clipboard via jQuery.



The code I have works if I use via on-click="Myfunction", but since I need to use it in several places, I would like to apply the function to all elements with a class. This is what I have tried.






$(".btn-clipboard").click(function(e)
copyToClipboard($(this));
);

function copyToClipboard(element)
var $temp = $("<textarea>");
$("body").append($temp);
$temp.val($(element).siblings("code").text()).select();
document.execCommand("copy");
$temp.remove();
;

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bd-clipboard">
<button class="btn-clipboard" title="" data-original-title="Copy to clipboard" >Copy</button>
</div>
<figure>
<pre>
<code class="language-html" data-lang="html"><br>
<span> code example that should be copied</span>
</code>
</pre>
</figure>












share|improve this question

















This question already has an answer here:



  • How to copy text from a div to clipboard

    5 answers



I am currently struggling with copying to clipboard via jQuery.



The code I have works if I use via on-click="Myfunction", but since I need to use it in several places, I would like to apply the function to all elements with a class. This is what I have tried.






$(".btn-clipboard").click(function(e)
copyToClipboard($(this));
);

function copyToClipboard(element)
var $temp = $("<textarea>");
$("body").append($temp);
$temp.val($(element).siblings("code").text()).select();
document.execCommand("copy");
$temp.remove();
;

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bd-clipboard">
<button class="btn-clipboard" title="" data-original-title="Copy to clipboard" >Copy</button>
</div>
<figure>
<pre>
<code class="language-html" data-lang="html"><br>
<span> code example that should be copied</span>
</code>
</pre>
</figure>







This question already has an answer here:



  • How to copy text from a div to clipboard

    5 answers






$(".btn-clipboard").click(function(e)
copyToClipboard($(this));
);

function copyToClipboard(element)
var $temp = $("<textarea>");
$("body").append($temp);
$temp.val($(element).siblings("code").text()).select();
document.execCommand("copy");
$temp.remove();
;

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bd-clipboard">
<button class="btn-clipboard" title="" data-original-title="Copy to clipboard" >Copy</button>
</div>
<figure>
<pre>
<code class="language-html" data-lang="html"><br>
<span> code example that should be copied</span>
</code>
</pre>
</figure>





$(".btn-clipboard").click(function(e)
copyToClipboard($(this));
);

function copyToClipboard(element)
var $temp = $("<textarea>");
$("body").append($temp);
$temp.val($(element).siblings("code").text()).select();
document.execCommand("copy");
$temp.remove();
;

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bd-clipboard">
<button class="btn-clipboard" title="" data-original-title="Copy to clipboard" >Copy</button>
</div>
<figure>
<pre>
<code class="language-html" data-lang="html"><br>
<span> code example that should be copied</span>
</code>
</pre>
</figure>






javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 14:28









Josh Mein

21.6k106881




21.6k106881










asked Nov 13 '18 at 14:18









and.richand.rich

1




1




marked as duplicate by Mohammad, Heretic Monkey, Foo, cнŝdk, Graham Nov 14 '18 at 3:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Mohammad, Heretic Monkey, Foo, cнŝdk, Graham Nov 14 '18 at 3:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • It i snot clear what exactly you want

    – brk
    Nov 13 '18 at 14:20











  • Not sure if it will make any difference, put you're passing in $(this) to a parameter element and then wrapping it up again as $(element)

    – freefaller
    Nov 13 '18 at 14:21

















  • It i snot clear what exactly you want

    – brk
    Nov 13 '18 at 14:20











  • Not sure if it will make any difference, put you're passing in $(this) to a parameter element and then wrapping it up again as $(element)

    – freefaller
    Nov 13 '18 at 14:21
















It i snot clear what exactly you want

– brk
Nov 13 '18 at 14:20





It i snot clear what exactly you want

– brk
Nov 13 '18 at 14:20













Not sure if it will make any difference, put you're passing in $(this) to a parameter element and then wrapping it up again as $(element)

– freefaller
Nov 13 '18 at 14:21





Not sure if it will make any difference, put you're passing in $(this) to a parameter element and then wrapping it up again as $(element)

– freefaller
Nov 13 '18 at 14:21












1 Answer
1






active

oldest

votes


















-1














As with some other actions in a browser (like opening a new window), the copy to clipboard can only be done via a specific user action (like a mouse click). For example, it cannot be done via a timer.



Here's a code example:






document.getElementById("copyButton").addEventListener("click", function() 
copyToClipboard(document.getElementById("copyTarget"));
);

function copyToClipboard(elem)
input
width: 400px;

<input type="text" id="copyTarget" value="Text to Copy"> <button id="copyButton">Copy</button><br><br>
<input type="text" placeholder="Click here and press Ctrl-V to see clipboard contents">








share|improve this answer


















  • 1





    syntax error: unexpected token: '{'

    – Jean-Marc Zimmer
    Nov 13 '18 at 14:30


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














As with some other actions in a browser (like opening a new window), the copy to clipboard can only be done via a specific user action (like a mouse click). For example, it cannot be done via a timer.



Here's a code example:






document.getElementById("copyButton").addEventListener("click", function() 
copyToClipboard(document.getElementById("copyTarget"));
);

function copyToClipboard(elem)
input
width: 400px;

<input type="text" id="copyTarget" value="Text to Copy"> <button id="copyButton">Copy</button><br><br>
<input type="text" placeholder="Click here and press Ctrl-V to see clipboard contents">








share|improve this answer


















  • 1





    syntax error: unexpected token: '{'

    – Jean-Marc Zimmer
    Nov 13 '18 at 14:30
















-1














As with some other actions in a browser (like opening a new window), the copy to clipboard can only be done via a specific user action (like a mouse click). For example, it cannot be done via a timer.



Here's a code example:






document.getElementById("copyButton").addEventListener("click", function() 
copyToClipboard(document.getElementById("copyTarget"));
);

function copyToClipboard(elem)
input
width: 400px;

<input type="text" id="copyTarget" value="Text to Copy"> <button id="copyButton">Copy</button><br><br>
<input type="text" placeholder="Click here and press Ctrl-V to see clipboard contents">








share|improve this answer


















  • 1





    syntax error: unexpected token: '{'

    – Jean-Marc Zimmer
    Nov 13 '18 at 14:30














-1












-1








-1







As with some other actions in a browser (like opening a new window), the copy to clipboard can only be done via a specific user action (like a mouse click). For example, it cannot be done via a timer.



Here's a code example:






document.getElementById("copyButton").addEventListener("click", function() 
copyToClipboard(document.getElementById("copyTarget"));
);

function copyToClipboard(elem)
input
width: 400px;

<input type="text" id="copyTarget" value="Text to Copy"> <button id="copyButton">Copy</button><br><br>
<input type="text" placeholder="Click here and press Ctrl-V to see clipboard contents">








share|improve this answer













As with some other actions in a browser (like opening a new window), the copy to clipboard can only be done via a specific user action (like a mouse click). For example, it cannot be done via a timer.



Here's a code example:






document.getElementById("copyButton").addEventListener("click", function() 
copyToClipboard(document.getElementById("copyTarget"));
);

function copyToClipboard(elem)
input
width: 400px;

<input type="text" id="copyTarget" value="Text to Copy"> <button id="copyButton">Copy</button><br><br>
<input type="text" placeholder="Click here and press Ctrl-V to see clipboard contents">








document.getElementById("copyButton").addEventListener("click", function() 
copyToClipboard(document.getElementById("copyTarget"));
);

function copyToClipboard(elem)
input
width: 400px;

<input type="text" id="copyTarget" value="Text to Copy"> <button id="copyButton">Copy</button><br><br>
<input type="text" placeholder="Click here and press Ctrl-V to see clipboard contents">





document.getElementById("copyButton").addEventListener("click", function() 
copyToClipboard(document.getElementById("copyTarget"));
);

function copyToClipboard(elem)
input
width: 400px;

<input type="text" id="copyTarget" value="Text to Copy"> <button id="copyButton">Copy</button><br><br>
<input type="text" placeholder="Click here and press Ctrl-V to see clipboard contents">






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 14:29









Ilia TapiaIlia Tapia

169




169







  • 1





    syntax error: unexpected token: '{'

    – Jean-Marc Zimmer
    Nov 13 '18 at 14:30













  • 1





    syntax error: unexpected token: '{'

    – Jean-Marc Zimmer
    Nov 13 '18 at 14:30








1




1





syntax error: unexpected token: '{'

– Jean-Marc Zimmer
Nov 13 '18 at 14:30






syntax error: unexpected token: '{'

– Jean-Marc Zimmer
Nov 13 '18 at 14:30






Popular posts from this blog

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo