I have a form where I have a table with an employee and every time I click on plus a new row for employee is added but when I click the remove field the script add a new row till the maximum. So what I want is the "-" icon to remove the field added.
This is what I came up with so far ...
$(document).ready(function () {
// Input fields increment limitation
var maxField = 3;
// Add button selector
var addButton = $('.add_button');
// Input field wrapper
var emp = $('#employee_tbl');
// New input field html
var fieldHTML = '<tr><td><label for="employee"><i class="fas fa-minus-circle"></i></label><input type="text" name="employee" class="remove_button" id="employee" required></td></tr>';
// Initial field counter is 1
var x = 1;
// Once add button is clicked
$(addButton).click(function () {
// Check maximum number of input fields
if (x < maxField) {
// Increment field counter
x++;
// Add field html
$(emp).append(fieldHTML);
}
});
// Once remove button is clicked
$(emp).on('click', '.remove_button', function (e) {
e.preventDefault();
// Remove field html
$(this).parent('div').remove();
// Decrement field counter
x--;
});
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet"/>
<table id="employee_tbl">
<tr>
<th>
<b><span data-i18n-key="employee">Employee</span></b>
</th>
</tr>
<tr>
<td>
<label for="employee"><i class="fas fa-plus-circle"></i></label>
<input type="text" name="employee" class="add_button" id="employee" required/>
</td>
</tr>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>