I'm working on dynamic input type but got stuck. The idea is whenever I changed input value,it'll create new row of input type. here's the image
for the first row I succeeded to create a new row like this
but when I try to change value on second input value,it not creating a input type. I don't know what is wrong here.
here's my code
var X = 1;
function tes() {
var wrapper = $('.receive-item');
X++;
$(wrapper).append(
'<tr>' +
'<td><input type="text" class="form-control form-control-sm item_code_' + X + '" id="item_code_' + X + '" name="item_code[]" placeholder="Item Code"></td>' +
'<td><a href="javascript:void(0)" class="btn btn-sm btn-danger remove" title="Delete"><i class="fas fa-minus"></i></a></td>' +
'</tr>'
);
$(wrapper).on('click', '.remove', function(e) {
e.preventDefault();
// if(!confirm("Delete this row?")) return;
$(this).closest("tr").remove();
});
}
$(document).ready(function() {
$('#item_code_' + X).on('change', function(e) {
tes();
});
});
$(document).ready(function() {
// $('.name_'+X).on('change',tes);
$('.addButton').click(tes);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" />
<div class="row">
<div class="col-md-6 field-wrapper">
<h5>List Receive Item</h5>
<div class="form-group">
<div class="table-responsive">
<table class="table table-hover table-bordered mb-0 text-dark">
<thead>
<tr>
<th>Item Code</th>
<th>Action</th>
</tr>
</thead>
<tbody class='receive-item'>
<tr>
<td><input type="text" class="form-control form-control-sm item_code_1" id="item_code_1" name="item_code[]" placeholder="Item Code"></td>
<td><a href="javascript:void(0)" class="btn btn-sm btn-primary addButton" title="Add Receive Items"><i class="fas fa-plus"></i></a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>