Copy to clipboard with jQuery (from a html tag ) [duplicate]
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>
javascript jquery
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.
add a comment |
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>
javascript jquery
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 parameterelement
and then wrapping it up again as$(element)
– freefaller
Nov 13 '18 at 14:21
add a comment |
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>
javascript jquery
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
javascript jquery
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 parameterelement
and then wrapping it up again as$(element)
– freefaller
Nov 13 '18 at 14:21
add a comment |
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 parameterelement
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
add a comment |
1 Answer
1
active
oldest
votes
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">
1
syntax error: unexpected token: '{'
– Jean-Marc Zimmer
Nov 13 '18 at 14:30
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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">
1
syntax error: unexpected token: '{'
– Jean-Marc Zimmer
Nov 13 '18 at 14:30
add a comment |
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">
1
syntax error: unexpected token: '{'
– Jean-Marc Zimmer
Nov 13 '18 at 14:30
add a comment |
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">
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">
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
add a comment |
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
add a comment |
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 parameterelement
and then wrapping it up again as$(element)
– freefaller
Nov 13 '18 at 14:21