I am trying to create a design in which I need to create a form row with a delete button dynamically.
Basic requirements-
- Each row has a select dropdown which is populated from a static array on the addition of the row.
- All the dropdowns have the same set of options.
- Each row has a delete button on the click it removes the whole row
Design
Current Implementation
HTML
<div id="ec-loanTypeContainer">
<div id="ec-add-loan-wrapper">
<div class="row select-row ec-border">
<div id="ec-loan-type" class="col-md-4 mb-5 mb-md-0">
<p class="ec-loan-type-label">Loan Type</p>
<select class="form-select form-control ec-loan-type" id="ec-loan-type-dropdown"
aria-label="Default select example">
<option class="ec-option-placeholder" value="" disabled selected>Select Loan Type
</option>
</select>
</div>
<div class="col-md-4">
<p>Monthly Installment (SGD)</p>
<div class="form-group mt-2">
<input type="text" class="ec-form-control" id="ec-monthly-installment"
aria-describedby="emailHelp" placeholder="E.g. 12,345.00" />
</div>
</div>
<div class="col-md-4">
<p>Outstanding Amount (SGD) </p>
<div class="form-group mt-2">
<input type="text" class="ec-form-control" id="ec-outstanding-amount"
aria-describedby="emailHelp" placeholder="E.g. 12,345.00" />
</div>
</div>
</div>
</div>
Javascript/Jquery
const optionItems = [{
value: "EX1",
text: "Example1"
}, {
value: "EX2",
text: "Example2"
}, {
value: "EX3",
text: "Example3"
}]
//Append a row of select and input on click of add button
$('#ec-add-another-loan').on("click", function() {
if (count <= 10) {
$('#ec-add-loan-wrapper').append('<div class="row ec-select-row ec-border"><div class="col-md-4 mb-5 mb-md-0"><select name="ec-loan-type[]" class="form-select form-control ec-loan-type" aria-label="Default select example"><option class="ec-option-placeholder" value="" disabled selected>Select Loan Type</option></select></div><div class="col-md-4"><div class="form-group mt-2"><input type="text" name="ec-monthly-installment[]" class="ec-form-control" aria-describedby="emailHelp" placeholder="E.g. 12,345.00" /></div></div><div class="col-md-4"><div class="form-group mt-2 ec-outstanding-amount-container"><input type="text" class="ec-form-control" aria-describedby="emailHelp" placeholder="E.g. 12,345.00" /><img src="../iwov-resources/assets/icons/icon-delete.svg" class="redTip img-fluid ec-delete-icon"/></div></div> </div > ')
count++;
}
//Append options dynamically
$.each(optionItems, function(i, item) {
$("select[name=ec-loan-type[" + count + "]]").append($('<option>', {
value: item.value,
text: item.text,
}));
});
});
//Delete the row on click of the delete icon
$('.ec-delete-icon').click(function() {
console.log("fsfssfsf", this)
$(this).parent().remove();
});
Current issues with the code
- Unable to figure out how to populate the select dropdown dynamically
- Unable to figure out how to find the delete icon for the specific row and delete the row.
Would be great if someone can help with this.