Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
onclick= copyToClipboard: for php variable using JavaScript

So I use the following code in php to copy any div, but how can I edit the js code to copy a php variable in the page, like $bookid or $row[1] (or copy/echo two strings like $bookid . $row[1] with one click) etc.?

<button onclick="copyToClipboard('#div1')\">Copy</button>

js code:

function copyToClipboard(element) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val($(element).text()).select();
  document.execCommand("copy");
  $temp.remove();
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The text that gets copied here, is the content of the dynamically created input field. Currently that gets set by reading the text content of element - so that is the part that needs replacing.

function copyToClipboard(textToCopy) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val(textToCopy).select();
  document.execCommand("copy");
  $temp.remove();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="copyToClipboard('hello world')">Copy</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!