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

131
Views
Combining multiple actions in onClick

I have built a "snippets" HTML document in which I have dozens various snippets with their codes in a "readonly" textarea. So the snippets may be easily copied inthe clipboard and pasted into a webpage content. I don't know, how to pack the whole "onClick" content into one function. This would save a lot of code in the whole "snippets" document, instead of using this onClick="" in each included snippet. This is my code:

<h2><em>Snippet:</em> Intro page - bold text</h2>
<form>
  <input title="One click transfers the code to the clipboard" type="button" value="Hilight & copy the code" onClick="this.form.code.select(),this.value = 'Hilighted code copied', document.execCommand('copy')">
  <textarea name="code" readonly="readonly">
    <!-- SNIPPET START -->
    *snippet code here*
    <!-- SNIPPET END -->
    </textarea>
</form>

Snippet block example on a HTML Snippets page

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your code should use ; instead of , but if you have more than one snippet, you should delegate

document.getElementById("container").addEventListener("click", function(e) {
  let tgt = e.target.closest("input");
  if (tgt && tgt.matches(".copy")) {
    const textarea = tgt.nextElementSibling;
    textarea.select();
    tgt.value = 'Hilighted code copied';
    document.execCommand('copy')
  }
  else tgt = e.target.closest("textarea");
  if (tgt.matches("[name=code]")) tgt.select()

})
<div id="container">
  <h2><em>Snippet:</em> Intro page - bold text</h2>
  <input class="copy" title="One click transfers the code to the clipboard" type="button" value="Hilight & copy the code" />
  <textarea name="code" readonly="readonly">
<!-- SNIPPET START -->
*snippet code 1 here*
<!-- SNIPPET END -->
</textarea>
  <h2><em>Snippet:</em> Intro page - bold text</h2>
  <input class="copy" title="One click transfers the code to the clipboard" type="button" value="Hilight & copy the code" />
  <textarea name="code" readonly="readonly">
<!-- SNIPPET START -->
*snippet code 2 here*
<!-- SNIPPET END -->
</textarea>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

Solved. The document must be *.PHP. The variable shall be declared, e.g.:

<?php 
$BTN = '<input class="copyBtn" title="Click to hilight & copy" type="button" value="Hilight & copy the code" />';
?>

The usage then is:

<?php echo $BTN;?>
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!