Note that there are some explanatory texts on larger screens.

plurals
  1. POClicked handler for dynamically created buttons in jquery
    primarykey
    data
    text
    <p>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.</p> <p>I want to be able to capture the click on the remove ("-") button, and delete that row from the table.</p> <pre><code>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 | &lt;input&gt; | + | | 2 | Test2 | &lt;input&gt; | + | //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 | &lt;input&gt; | + | | 1 | | &lt;input&gt; | - | | 2 | Test2 | &lt;input&gt; | + | //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"); }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload