Tuesday, 27 August 2013

Remove button next to each div

Remove button next to each div

Here add person button add a set of text fields to add the details of the
person.
And I have given Remove option to remove the respective .loop div, it
removes the .loop div but when the first set is removed then Add Person
button is again not adding the .loop div because since the .loop div is
completely removed from the page, it is not finding that div to clone. How
do I solve this?
Code here
$(document).ready(function() {
clicks = 0;
$('div.test').on('click', function () {
$('.attnd2').show();
$('div.loop').show();
if ($('div.loop').length < 5) {
clicks++;
if(clicks>1){
newLoopDiv =
$($('div.loop')[0]).clone().appendTo(".container");
$('input', newLoopDiv).each(function (index,
element) {
$(element).attr('name', $(element).attr('name') +
clicks);
});
}
else{
newLoopDiv = $($('div.loop')[0]).appendTo(".container");
$('input', newLoopDiv).each(function (index,
element) {
$(element).attr('name', $(element).attr('name') +
clicks);
});
}
}
$( ".remove" ).click(function() {
$(this).closest('.loop').remove();
});
});
$( ".remove" ).click(function() {
$(this).closest('.loop').remove();
});
});
FIDDLE

No comments:

Post a Comment