Reputation: 151
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
Reputation: 11327
Change .clone()
to .clone(true)
to also clone event handlers.
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