0

I do have a button to clone a input field. This input field should trigger a value in a second field after the own value was changed.

But the problem is it does only work on the first one and not on the cloned one.

$(".products:last #automenge").on("change", function() {});

It does work to change the value of the last like:

$(".products:last #autopreisges").val(gesamptpreis);

But the change function seem to not work like that.

Now working snippet !

var products = $(".products").clone();

$('#clone-search').click(function() {
  var clonedRow = products.clone().insertAfter(".products:last").find(":input").val(this.value).end();

});

$(".products:last .clautomenge").on("change", function() {
  alert("wow");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="products">
  <input class="clautomenge" type="text" value="1" name="Menge[]" placeholder="Menge" required />
</div>

<button id="clone-search" class="formstylebutton" type="button">
    add one
</button>

A fiddle: https://jsfiddle.net/awq7sy69/

delato468
  • 474
  • 4
  • 18
  • 1
    http://api.jquery.com/clone/ read the documentation, look at the arguments that you can pass into clone. – Taplar Nov 28 '18 at 15:20
  • Please add a [mcve] which shows the actual proble _in the question itself_ and not only a link to an external resource. – Andreas Nov 28 '18 at 15:20
  • It also looks like you are cloning an element with an id in the process and not changing that id, which is creating invalid markup. Not your original issue, but that's making non-standard html. – Taplar Nov 28 '18 at 15:22
  • You can't repeat ID's in a page. They are unique by definition – charlietfl Nov 28 '18 at 15:22
  • You're also cloning every existing `.products` element when the button is clicked, instead of your assumed intent of cloning a single instance – Rory McCrossan Nov 28 '18 at 15:28
  • @Taplar the clone was never the problem and i do also removed the id now. – delato468 Nov 28 '18 at 15:32
  • @Rory McCrossan your right thats just a small example but my code also contains more fields that have to be cloned. – delato468 Nov 28 '18 at 15:32
  • The snippet is now working also on stackoverflow. – delato468 Nov 28 '18 at 15:33

0 Answers0