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

150
Views
Cloning elements with their events not working

I am cloning a div using jQuery and find that events on cloned elements do not work although they work on the original elements. How can I go about this? Note how the answer in the question states that...

"This is how jQuery's clone() method is able to copy a node with its event listeners, for example"

let add = document.getElementsByClassName("add"),
  rem = document.getElementsByClassName("rem"),
  container = document.getElementsByClassName('container')[0];
for (let i = 0; i < add.length; i++) {
  add[i].addEventListener("click", () => {
    $(".cloneable").clone(true, true).appendTo(".container");
  });
}
for (let i = 0; i < rem.length; i++) {
  rem[i].addEventListener("click", () => {
    if (container.childElementCount > 1) {
      $(".cloneable:last").remove();
    }
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="cloneable">
    <div>
      <a class="btn btn-primary add" role="button">Add cell</a>
      <a class="btn btn-primary rem" role="button">Remove cell</a>
    </div>
    <iframe src="index.php"></iframe>
  </div>
</div>

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

ETA: This code clones all elements named ".cloneable", so on second click it creates TWO clones, and so on. You should clone with $('.cloneable:first') or similar.

The issue seems to arise from you binding javascript native events. Using jQuery Events solves your problem.

let add = document.getElementsByClassName("add"),
  rem = document.getElementsByClassName("rem"),
  container = document.getElementsByClassName('container')[0];
for (let i = 0; i < add.length; i++) {
  $(add[i]).on("click", () => {
    $(".cloneable:first").clone(true, true).appendTo(".container");
  });
}
for (let i = 0; i < rem.length; i++) {
  $(rem[i]).on("click", () => {
    if (container.childElementCount > 1) {
      $(".cloneable:last").remove();
    }
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="cloneable">
    <div>
      <a class="btn btn-primary add" role="button">Add cell</a>
      <a class="btn btn-primary rem" role="button">Remove cell</a>
    </div>
    <iframe src="index.php"></iframe>
  </div>
</div>

about 4 years ago · Santiago Trujillo 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!