Using .delegate() method for table elements

Using .delegate() method for table elements

Hello all,

I've an HTML table that uses the jQuery-datatables plug-in.
One of the columns in the table includes a button for adding a new row to the table immediately following the row where the button was clicked from.

My HTML code is as follows :

  1. <table id="contactInfo">
  2. <thead>...</thead>
  3. <tbody>
  4. <tr>
  5. <td>...</td>
  6. <td><button type="button" class="addRow">+</button></td>
  7. <td>...</td>
  8. </tr>
  9. </tbody> 
  10.  </table>
  11. And my jQuery code for firing the corresponding onclick event handler for the button is as follows:

  1. $(function() {
  2.     // HTML template of a row
  3.     var html = '<tr><td> </td> <td> </td> <td> </td> <td> 1 </td> <td> 2 </td> </tr>';

  4.     $('#contactInfo').delegate("button.addRow", "click" , function() {
  5.         alert("button clicked");
  6.         var row = $(this).closest(tr); // get the parent row of the clicked button
  7.         $(html).insertAfter(row); // insert content
  8.     }); 
});   

With this code, the function() to execute my button's onclick event isn't loading and therefore I cant event see the "button clicked" alert msg.

Any ideas what I'm wrong, or any alternative would be of great help.

-Pritish.