Delete along with the corresponding <span>
I am experimenting with dynamic forms. I created a text box with an add button and a div. Whenever the user enters something in this text box and presses the add button, this value falls into the div with the dynamic span tag. I also provided editing and deletion options. When the delete option is clicked, the entire range must be deleted. This is my logic.
Here is how I tried:
$("#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);
});
}
});
Whenever I click the "Delete" button, it is deleted <span>, leaving behind <br>in the same line. I have provided a line at the end of each span, so when a user enters a new value in a text field, a span with that value will go to the next line.
, , . , . - ?
, <br/> JavaScript CSS span .
$('.remove').click(function () {
removebuttonid = $(this).attr('id'); // check this var is already defined
var spanid = removebuttonid.replace('remove', 'span');
$('#' + spanid).remove();
});
p span, .
, JavaScript href='javascript:void(0) a a:hover css:
a:hover {
cursor: pointer;
}