0

I know how to copy the value of the input, as shown below, but when I try to do the same in a paragraph - for example, it doesn't work, let me know how to solve it?

$(function() {
  $('input').click(function() {
    $(this).focus().select();
    document.execCommand("copy");
  });

  $('p').click(function() {
    $(this).select();
    document.execCommand("copy");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' readonly value='click to copy me'>

<p>This is a pararaph</p>
Aditi Parikh
  • 1,522
  • 3
  • 13
  • 34
  • 1
    Possible duplicate of [How do I copy to the clipboard in JavaScript?](https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript) – Nir Tzezana Oct 02 '17 at 10:28
  • 1
    It does work when you double click on the p element. – Dream_Cap Oct 02 '17 at 10:32
  • no i didnot duplicate this – Ramez GaIal Oct 02 '17 at 10:34
  • now i realized that it works by dbl click :D two letters to copy better than nothing :D thanks – Ramez GaIal Oct 02 '17 at 10:35
  • Possible duplicate of [Can you use document.execCommand('copy') on the contents of a div?](https://stackoverflow.com/questions/43687696/can-you-use-document-execcommandcopy-on-the-contents-of-a-div) – freedomn-m Oct 02 '17 at 10:36
  • what does "Possible duplicate of..." mean? – Ramez GaIal Oct 02 '17 at 10:37
  • It means a very similar question has been asked before and we've provided a link to a possible answer. Based on the user's reputation, the question may be closed immediately (eg if Rory did it) or you get a vote-to-close option. If enough people agree that this question is the same as the linked question, then this one is closed as a duplicate. – freedomn-m Oct 02 '17 at 10:38
  • oh it's been for days since i am on this website, seems i mistakenly asked that question, so do i have to delete my question ? – Ramez GaIal Oct 02 '17 at 10:40
  • Possible duplicate of [Click button copy to clipboard using jQuery](https://stackoverflow.com/questions/22581345/click-button-copy-to-clipboard-using-jquery) – Yashar Aliabbasi Oct 02 '17 at 11:02

1 Answers1

0

Here's a simple script to solve that issue: http://id0t.x10.mx/dl/clipboard.js

Take a look at the script's source for a tutorial on how to use it.

A feature that I added was the true parameter on all the functions. This parameter specifies if the tooltip should be displayed.

Ex.

<button onclick="select_all_and_copy(document.querySelector('span'),true)">Click to copy the span!</button>

<span>Here's some text.</span>
ayunami2000
  • 441
  • 6
  • 10