I used below JS & HTML to remove row on clicking of close icon. It works fine.
But, need to add additional 2 logics. Maximum 9 rows.
https://jsfiddle.net/jkenluv/4cj6qnye/3/
- if item2 removed and item3 exists, item3 'id' have to change as item2.
- If item2 removed on click close. If user clicks 'Add' again.. it have with item2. Not item3. What so ever case, sequence have to follow... item1, item2, item3, item4, item5, item6, item7, item8, item9.
JS:
var i = 2;
$("#addMoreNames").click(function() {
var temp_id = "add-name"+i;
var firstNameInput = $('<div />', {'class': 'col-lg-6 col-md-6 col-sm-6 campaign-firstname form-validate', 'data-campaign-contact-id': i}).append('<div class="form-title">Given name</div>').append($("<input />", { type: "text", id:"campaign-firstname-"+i, class: "name-validator", name: "First Name", "data-firstname": "First name is missing" }));
var lastNameInput = $("<div />", {'class': 'col-lg-5 col-md-5 col-sm-5 campaign-lastname form-validate', 'data-campaign-contact-id': i}).append('<div class="form-title">Family name</div>').append($("<input />", { type: "text", id:"campaign-lastname-"+i, class: "name-validator", name: "Last Name", "data-lastname": "Last name is missing" }));
$("<div />", { "class":"row-names", id:"add-name"+i })
.append(firstNameInput[0])
.append(lastNameInput[0])
.append('<div class="col-lg-1 col-md-1 col-sm-1 remove-flight"><a href="javascript:void(0);" aria-label="Remove">X</a></div>')
.appendTo("#add-more--names");
$('.remove-flight a').on('click', function(e){
e.preventDefault();
$(this).parent().remove();
});
i++;
if (i < 10){
$(this).show();
} else {
$(this).hide();
}
Thanks
#addMoreNames
you're adding a new click handler on every.remove-flight a
link:$('.remove-flight a').on('click', ...
– Andreas