4

I have the following code to copy a text to the clipboard by clicking on a Button. Text is in a Paragraph element, So I move that text to a hidden input field and then copy it to the clipboard. But this is only working if I move the text to a text field but not a hidden field. I also tried to display:none the input field, but the result is the same. (I can't set it to visibility:hidden because the space matters). How can I solve this?

$("button").on("click", function() {
  var n = $("p").text();
  n = $.trim(n);
  $(".copied").attr("value", n).select();
  document.execCommand("copy");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>
This is the copied text!
</p>
<input type="hidden" class="copied"/>
<button>
COPY
</button>
<input type="text" placeholder="paste copied text here"/>

Here is the editable jsfiddle:

http://jsfiddle.net/d9a4x6vc/

David Johns
  • 1,254
  • 3
  • 19
  • 48
  • 2
    It should work with `opacity: 0; position: absolute; z-index: -1` –  Dec 13 '18 at 12:37
  • 1
    You might also want to add `pointer-events: none`. Or use `position: fixed; top: -100px` or something, to move it completely off screen. –  Dec 13 '18 at 12:45

3 Answers3

3

You can try to change the type of the input to text before select then, and bring the type hidden back after like that.

$("button").on("click", function() {
  var n = $("#copyMe").text();
  $(".copied").attr("value", n);
  $(".copied").attr("type", "text").select();
  document.execCommand("copy")
  $(".copied").attr("type", "hidden")
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="copyMe">
This is the copied text!
</p>
<input type="hidden" class="copied"/>
<button>
COPY
</button>
<input type="text" placeholder="paste copied text here"/>
Lucas
  • 275
  • 8
  • 37
1

I had exactly the same problem recently. What I did is put that input box position as absolute and moved it off screen. Also notice that even input field width does affect result. I tried to put width and height to 0, and it didn't copy after that also.

maximelian1986
  • 2,308
  • 1
  • 18
  • 32
  • Solved with the solution given by @Chris G mentioned in the question's comment `opacity: 0; position: absolute; z-index: -1` – David Johns Dec 13 '18 at 12:41
1

As DavidDomain explains in answer to a similar question, you need to change your input properties to take the value.

In your case, you can try this:

$("button").on("click", function() {
  var n = $("p").text();
  n = $.trim(n);
  $(".copied").css({
    position: "absolute",
    left: "-1000px",
    top: "-1000px"
  }).attr("value", n).attr("type","text").select();
  $(".copied").attr('css','').attr("type","hidden");
  document.execCommand("copy");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>
This is the copied text!
</p>
<input type="hidden" class="copied"/>
<button>
COPY
</button>
<input type="text" placeholder="paste copied text here"/>
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Renzo Calla
  • 7,486
  • 2
  • 22
  • 37