I've been experimenting with creating dynamic forms. My latest project involves a text box, an 'Add' button, and a div. The idea is that whenever a user types something into the text box and clicks the Add button, that value should appear in the div within a dynamically created span tag. I've also included options for editing and deleting these values. When the delete option is clicked, the entire span should be removed. This is the logic I'm working with.
This is my approach:
$("#AddBtn").click(function () {
if ($("#PickList_Options").val() == "") {
alert("Please enter an option");
} else {
if ($("#AddBtn").val() == "Add") {
var optionvalue = $("#PickList_Options").val();
$("#mydiv").append("  <span id='span" + a + "' class='editbutton'>" + optionvalue + "    <a href='javascript:void(0)' ><i class='fa fa-close btn btn-danger btn-xs btn-remove btn-icon remove' id='remove" + a + "' style='display: none;'></i></a><a href='javascript:void(0)'><i class='fa fa-pencil btn btn-warning btn-xs btn-edit btn-icon edit' id='edit" + a + "' style='display: none; margin-right:10px;'></i></a></span><br/>");
a = a + 1;
$("#PickList_Options").val("");
} else if ($("#AddBtn").val() == "Update") {
$("#" + spanid).text($("#PickList_Options").val()).append("    <a href='javascript:void(0)' ><i class='fa fa-close btn btn-danger btn-xs btn-remove btn-icon remove' id='" + removebuttonid + "' style='display: none;'></i></a><a href='javascript:void(0)'><i class='fa fa-pencil btn btn-warning btn-xs btn-edit btn-icon edit' id='" + editbuttonid + "' style='display: none; margin-right:10px;'></i></a>");
$("#AddBtn").val("Add");
$("#PickList_Options").val("");
}
$('.remove').click(function () {
removebuttonid = $(this).attr("id");
alert(removebuttonid);
var spanid = removebuttonid.replace('remove', 'span');
alert(spanid);
$("#" + spanid).remove();
});
$(".edit").click(function () {
addButtonValue = "Update"
$("#AddBtn").val(addButtonValue);
editbuttonid = $(this).attr("id");
alert(editbuttonid);
spanid = editbuttonid.replace('edit', 'span');
alert(spanid);
var value = ($("#" + spanid).text()).trim();
$("#PickList_Options").val(value);
});
}
});
When I click the delete button, the <span>
gets removed but leaves behind the <br>
in the same line. I have added a line break at the end of each span tag so that a new value will start on the next line.
The issue I'm facing is when the delete button is clicked, the
in that line should also be removed. I can't seem to figure out how to do this. Can someone offer some guidance on this matter?