0

Snippet:

$(document).ready(function() {
  $('button').click(function() {
    $('table').append("<tr><td id='no'>X</td><td>Example</td></tr>")
  });
  $('th').on('click', '#no', function() {
    $(this).remove();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <th>Delete</th>
    <th> Element</th>
  </tr>
  <tr>
    <td id='no'>X</td>
    <td>Example</td>
  </tr>
</table>
<br>
<button>
More
</button>

I want a row to be deleted when I click the X on that row. What code should I add to do so?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lucian cahil
  • 83
  • 1
  • 2
  • 8
  • Possible duplicate of [Remove table row after clicking table row delete button](https://stackoverflow.com/questions/11553768/remove-table-row-after-clicking-table-row-delete-button) – S4NDM4N Aug 30 '17 at 05:50

8 Answers8

1

Here you go with a solution https://jsfiddle.net/wfLu3Lzu/

$(document).ready(function(){
    $('button').click(function(){
        $('table').append("<tr><td id='no'>X</td><td>Example</td></tr>")
    });
    $('table').on('click', 'tr #no', function(){
        $(this).closest('tr').remove();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
    <th>Delete</th>
    <th> Element</th>
</tr>
<tr>
    <td id='no'>
        X
    </td>
    <td>Example</td>
</tr>
</table>
<br>
<button>
More
</button>

Event delegation should start from table till tr #no

Hope this will help you.

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
1

Instead of

$('th').on('click', '#no', function(){

You should be using event delegation:

$(document).on('click', '#no', function(){

Here is the working demo for you:

$(document).ready(function() {
  $('button').click(function() {
    $('table').append("<tr><td id='no'>X</td><td>Example</td></tr>")
  });
  $(document).on('click', '#no', function() {
    $(this).parent().remove();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <th>Delete</th>
    <th> Element</th>
  </tr>
  <tr>
    <td id='no'>
      X
    </td>
    <td>Example</td>
  </tr>
</table>
<br>
<button>
More
</button>
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
1

You keep appending elements with the same ID, that are dynamically inserted after the event handler is bound.

You can easily create the element, with the event handler, at the time it's inserted, and use a class for styling rather than duplicate ID's

$(document).ready(function() {
  $('#no').on('click', function() {
    $(this).closest('tr').remove();
  });
  
  $('button').click(function() {
    var tr  = $('<tr />'),
        td1 = $('<td />', {
          'class' : 'no',
          text    : 'X',
          on : {
            click : function() {
              tr.remove();
            }
          }
        }),
        td2 = $('<td />', {
          text : 'Example'
        });
  
    $('table').append(tr.append(td1, td2))
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <th>Delete</th>
    <th> Element</th>
  </tr>
  <tr>
    <td id='no'>
      X
    </td>
    <td>Example</td>
  </tr>
</table>
<br>
<button>
More
</button>
adeneo
  • 312,895
  • 29
  • 395
  • 388
0

$(document).ready(function(){
    $('button').click(function(){
      $('table').append("<tr><td onclick='removeRow(this)' id='no'>X</td><td>Example</td></tr>");
    });
});
function removeRow($this){
        $($this).parent().remove();
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
    <th>Delete</th>
    <th> Element</th>
</tr>
<tr>
    <td onclick='removeRow(this);'  id='no'>
        X
    </td>
    <td>Example</td>
</tr>
</table>
<br>
<button>
More
</button>
Farhad Bagherlo
  • 6,725
  • 3
  • 25
  • 47
0

You should follow event delegation for dynamically created html elements. check the bold code I have changed.

$(document).ready(function(){
    $('button').click(function(){
        $('table').append("<tr><td id='no'>X</td><td>Example</td></tr>")
    });
    **$(document)**.on('click', ''th' #no', function(){
        **$(this).parent('th:first').remove();**
    });
});
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
0

You X button is in td tag and you are trying to bind event on th tag.

can you please try this

$(document).ready(function(){
    $('button').click(function(){
        $('table').append("<tr><td id='no'>X</td><td>Example</td></tr>")
    });
    $('table').on('click', '#no', function(){
        $(this).parent().remove();
    });
});
0

Try this one:

$('tr').on('click', '#no', function(){
        $(this).closest('tr').remove();
    });
Rakib
  • 643
  • 7
  • 17
0

This function will locate the td cliacked and remove the tr it was belong.

 $("td").click(function(){
  $(this).closest("tr").remove();
 });

Hope it solved your porblem.

jek
  • 148
  • 1
  • 1
  • 12