Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  73] [ 5]  / answers: 1 / hits: 17278  / 7 Years ago, fri, may 12, 2017, 12:00:00

In my View, I'm dynamically add divs like this via JS:



<div id=detail[0]>
<input name=detail.Index type=hidden value=0 />
<input name=detail[0].details_name type=text />
<input name=detail[0].details_value type=text />
<input class=btn type=button value=Delete />
</div>


The question is how to delete the chosen one by clicking the Delete button in div?



JavaScript that adds new divs:



<script>
$(function() {
var i = 0;
$('.plus').click(function()
{
++i;
var html2add = <div id='detail[ + i + ]'> +
<input name='detail.Index' type='hidden' value=' + i + ' /> +
<input name='detail[ + i + ].details_name' type='text' /> +
<input name='detail[ + i + ].details_value' type='text' /> +
<input class='btn' type='button' value='Delete' />+
</div>;
$('#details_part').append(html2add);
})
})



More From » jquery

 Answers
142

To delete the div containing the delete button, just use the remove() function:



$(document).on('click', '.btn', function(){
var cur = $(this);
cur.parent().remove();
});

[#57809] Wednesday, May 10, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jacklynr

Total Points: 542
Total Questions: 120
Total Answers: 95

Location: Cape Verde
Member since Fri, Nov 27, 2020
4 Years ago
;