I create a table in html, then populate it in the below table (drawTable gets called in my document.ready function). For each row, there is a button at the end to add another row with the same id, inserted directly below. The clicked handler for the table(#fieldTable) td elements works fine for all the buttons initially inserted. When they click the "+" button, it adds the row with a "-" button at the end. Now this shows up fine on the screen, but when clicked, the tables td clicked handler doesnt get fired, but the documents does.
I want to be able to capture the click on the remove ("-") button, and delete that row from the table.
function drawTable() {
//fill a table I created in html, (not important for this question)
//it now looks like this
| ID | NAME | VALUE | ACTION |
| 1 | Test | <input> | + |
| 2 | Test2 | <input> | + |
//where the action column is a button (+ indicates create a new row)
//so when they click the grid this gets called
$('#fieldTable td').click(function() {
var row = $(this).parent().parent().children().index($(this).parent());
var col = $(this).parent().children().index($(this));
if(col != 3)
{
return;
}
var text = $(this).parents('tr').find('td:last').text();
var etiId = $(this).parents('tr').find('td:first').text();
console.log(text);
if(text == "+")
{
var $tr = $(this).closest('tr');
var $clone = $tr.clone();
$clone.find(':text').val('');
$clone.find('td:nth-child(2)').text('');
$clone.find('td:nth-child(4)').find('button').text('-');
$tr.after($clone);
}
//so now the grid would look like this
| ID | NAME | VALUE | ACTION |
| 1 | Test | <input> | + |
| 1 | | <input> | - |
| 2 | Test2 | <input> | + |
//the issue is, if I click the "-" button, this handler does not get called
// the document.on('click'...) does, but I am not sure how to determine the
// row/column of the button click and then remove that row
else if(text == "-")
{
console.log("remove");
$(this).parent().parent().remove();
}
});
$(document).on('click', '.btnAddRemove', function() {
console.log("document cliked");
});
}