Add button is working as expected but when I try to remove the cloned div I am only able to remove the last one but not all the cloned div one by one from last expect the default one.
Here is my Div and Jquery to add and remove div.
Add:
$("#AddSubService").click(function(){
$("#SubServices").append($('#SubServiceName').clone(true).find("input").val("").end());
$("#SubServices").append($('#SubServiceDescription').clone(true).find("input").val("").end());
});
Remove:
$("#RemoveSubService").click(function(e){
$("#SubServices").children("div[id=SubServiceDescription]:last").fadeOut();
$("#SubServices").children("div[id=SubServiceName]:last").fadeOut();
});
Div:
<div id="SubServices">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<h2>Sub Services</h2>
<input type="button" value="Add" class="btn btn-default" id="AddSubService" /> |
<input type="button" value="Remove" class="btn btn-default" id="RemoveSubService" />
</div>
</div>
<div class="form-group" id="SubServiceName">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group" id="SubServiceDescription">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
</div>
As I am pretty new in MVC and Jquery so any right direction to achieve the same would be highly appreciable.