Reputation: 527
I am dynamically, using jquery clone and append, creating a remoce button. When i clicked on remove button, i am trying to remove cloned section. I cannot even see alert when clicked on remove button.
var uniqueId = 1;
$('#AddCC').click( function() {
var copyDiv = $("#CCPanel").clone();
var divID = 'CCPanel' + uniqueId;
copyDiv.attr('id',divID);
var removeID = "removeCard";
$("#CCcontainer").append(copyDiv);
$("#CCcontainer").append("<input type=\"button\" value=\"Remove Card\" id=" + removeID + ">");
$('#' + divID).find('input,select').each(function(){
$(this).attr('id', $(this).attr('id') + uniqueId);
});
uniqueId++;
});
$("#removeCard").bind('click',function () {
alert("I am here");
if(uniqueId==1){
alert("No more textbox to remove");
return false;
}
uniqueId--;
$("#CCPanel" + uniqueId).remove();
});
Click add another Card button to clone the section and see remove button DEMO
Upvotes: 0
Views: 101
Reputation: 15112
Use event delegation
$(document).on('click','#removeCard',function () {
instead of
$("#removeCard").bind('click',function () {
Upvotes: 1