i need help
i need help

Reputation: 2386

Jquery remove table row not working

Basically is to click button to generate table row, then click minus button to remove row.

I have tried it with a few ways below, non of it work. -.bind -.live -normal way

It seems like it is not working due to the table is generated dynamically.

<script language="JavaScript" src="jquery-1.4.3.min.js" type="text/javascript"></script>

 <script type="text/javascript">

 $(document).ready(function() {    

  var html = '';
  var rows= 5;
  var cols= 4;

  $('#btnGenTable').bind("click", function() {

   for (var r = 1; r <= rows; r++) 
   {
    html += '<tr>';

    for (var c = 1; c <= cols; c++) 
    {
     cellText= "row " + r + " col " + c;
     html += '<td>aa</td>';
    }

    html+= '<td><img class="delete" src="minus.jpg" /></td>';

    html += '</tr>';
   };


   $('.inner').html('').append(html);

  });


$('table tbody tr td img.delete').click(function(){
    alert('clicked');
        $(this).parent().parent().remove();
});

 });

</script>

<br />


<table id="tblsize" class="inner" border="1" width="80%">
</table>


<input type=button value="Generate Table" name="btnGenTable" id="btnGenTable" class=text100>

Upvotes: 0

Views: 8142

Answers (3)

fruppel
fruppel

Reputation: 225

I know this question is old but I had the same problem. The live() method is deprecated now since jQuery version 1.7 and will be removed in version 1.9. You could use this instead:

$(document).on('click', '.delete', function(){
    $(this).closest('tr').remove();
});

Upvotes: 2

Patricia
Patricia

Reputation: 7802

all you need is:

  $('.delete').live('click', function(){
          $(this).closest('tr').remove();   
      });

in your document ready handler.

here's a working example: http://jsfiddle.net/5bWcT/

Upvotes: 1

Nikolaus Gradwohl
Nikolaus Gradwohl

Reputation: 20124

To remove a row when the image is clicked insert the following code in your $(document).ready block.

$('img.delete').live('click', function() {
    $(this).parent().parent().remove();
});

Upvotes: 2

Related Questions