user1100603
user1100603

Reputation: 151

JQuery - Clone table on first textfield .change event

The .change clone works for the first field, but not for the second. What can I add to my loop?

Thanks.

<table>
  <tr> 
     <td><input type="text" id="txtA" name="txtA"></td> 
     <td><input type="text" id="txtB" name="txtB"></td> 
  </tr>
</table>

<script>
    var i = 1;
    $("#txtA").change(function() {
       $("table tr:first").clone().find("input").each(function() {
          $(this).val('').attr('id', function(_, id) { return id + i });
       }).end().appendTo("table");
       i++;
    });
</script>

Upvotes: 3

Views: 188

Answers (1)

RightSaidFred
RightSaidFred

Reputation: 11327

Change .clone() to .clone(true) to also clone event handlers.

JSFIDDLE DEMO


If you meant that you want the change handler to be bound to both inputs, then just add the ID of the second element to the selector:

$("#txtA,#txtB").change(...

Upvotes: 4

Related Questions