Here is the code: http://jsfiddle.net/cwWWN/
I'm still fresh to jQuery. All I'm wanting to do is have an Add and Delete button that'll add a row when the Add is pushed and Delete the row that the button is clicked on. For some reason, the Add button stops working after the first append. If I do a clone, like the commented out code, the Add button stops working after the first Delete. Any ideas?
HTML:
<table border="1" name="phoneTable" id="phoneTable" width="100%">
<thead>
<tr>
<th> Primary </th>
<th> Type </th>
<th> Phone Number</th>
<th> Details </th>
<th> Add </th>
<th> Delete </th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="radio" name="phonePrimary[]"/></td>
<td>
<select style="" name="phoneType[]">
<option value="1">NOT COMPLETED</option>
<option value="2">MOBILE</option>
<option value="3">HOME</option>
<option value="4">WORK</option>
<option value="5">NEAR BY</option>
</select>
</td>
<td><input type="text" style="width: 200px;" name="phoneNumber[]" /></td>
<td><input type="text" name="phoneDetails[]" /></td>
<td><button type="button" id="addButton">+ Add</button></td>
<td><button type="button" id="deleteButton">- Delete </button></td>
</tr>
</tbody>
JS:
$("#addButton").on("click", function () {
alert("Clicked the add! ");
$('#phoneTable tr:last').after('<tr><td><input type="radio" name="phonePrimary[]"/></td><td><dropdown:DropdownPhoneType entities="${applicationBean.phoneTypes}" name="phoneType[]"/></td><td><input type="text" placeholder="(999) 999-9999" class="phone-mask textfield" data-tooltip="Please enter a valid <b>Number</b>" style="width: 200px;" name="phoneNumber[]" /></td><td><input type="text" name="phoneDetails[]" /></td><td><button class="button button-black" type="button" id="addButton">+ Add</button></td><td><button class="button button-black" type="button" id="deleteButton">- Delete </button></td></tr>');
//$('#phoneTable tbody > tr:last').clone(true, true).appendTo("#phoneTable");
$('#phoneTable tbody > tr:last').prev().find("button[id='addButton']").remove();
});
$("#deleteButton").on("click", function () {
if($("#phoneTable tbody > tr").length <= 1) {
return false;
}
$('#phoneTable tbody > tr:nth-last-child(2) > td:nth-last-child(2)').append('<button type="button" id="addButton">+ Add</button>');
$
$('#phoneTable tbody > tr:last').fadeOut('slow', function () {
$('#phoneTable tbody > tr:last').remove();
});
//alert('Delete Clicked');
});
.on() doesn't work like you think it does. If you want the event to be delegated (in old terms, live), you need to bind the event to a parent, and pass it an optional selector parameter. So for any elements that are added dynamically after the hander is initially bound, you need to bind your events like so:
$('body').on('click', '#addButton', function() {
// code here
});
See: http://api.jquery.com/on/
The reason why your new ADD button is not working is that you don't add the click event listener when you are creating your new ADD button.
Fiddle example
From the Fiddle:
function addRow() {
alert("Clicked the add! ");
$('#phoneTable tr:last').after('<tr><td><input type="radio" name="phonePrimary[]"/></td><td><dropdown:DropdownPhoneType entities="${applicationBean.phoneTypes}" name="phoneType[]"/></td><td><input type="text" placeholder="(999) 999-9999" class="phone-mask textfield" data-tooltip="Please enter a valid <b>Number</b>" style="width: 200px;" name="phoneNumber[]" /></td><td><input type="text" name="phoneDetails[]" /></td><td><button class="button button-black" type="button" id="addButton">+ Add</button></td><td><button class="button button-black" type="button" id="deleteButton">- Delete </button></td></tr>');
//$('#phoneTable tbody > tr:last').clone(true, true).appendTo("#phoneTable");
$('#phoneTable tbody > tr:last').prev().find("button[id='addButton']").remove();
$("#addButton").on("click", addRow);
}
$("#addButton").on("click", addRow);
Just do the same thing for the remove Button.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With