views:

31

answers:

2

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>
A: 

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();
});
Nikolaus Gradwohl
+1  A: 

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/

Patricia
jsfiddle.net is such a great tool
i need help
yea, it's so handy.
Patricia