I am creating a scenario where a user can add a new line with the "Delete" option by clicking the "Add" button. If they click the Delete button, the entire line will be deleted. I put a script on it. But this does not work perfectly. Please take a look at my fiddle: http://jsfiddle.net/learner73/gS8u2/
My problem:
(1) If the user clicks the Add button, a new row will be added. It's good. But he dropped below the Add button. I want a new line to be added above the Add button, I mean that the Add button should always be at the bottom.
(2) In my code, if the user clicks the Delete button in a previously created line, the entire line will be deleted. It's good. But when they click the "Delete" button on the dynamically created line, nothing will happen!
HTML:
<div class="optionBox">
<div class="block">
<input type="text" /> <span class="remove">Remove Option</span>
</div>
<div class="block">
<input type="text" /> <span class="remove">Remove Option</span>
</div>
<div class="block">
<span class="add">Add Option</span>
</div>
</div>
JQuery
$('.add').click(function() {
$('.optionBox').append('<input type="text" /><span class="remove">Remove Option</span>');
});
$('.remove').click(function() {
$(this).parent('div').remove();
});
source
share