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 :
- <table id="contactInfo">
- <thead>...</thead>
- <tbody>
- <tr>
- <td>...</td>
- <td><button type="button" class="addRow">+</button></td>
- <td>...</td>
- </tr>
- </tbody>
- </table>
- And my jQuery code for firing the corresponding onclick event handler for the button is as follows:
- $(function() {
- // HTML template of a row
- var html = '<tr><td> </td> <td> </td> <td> </td> <td> 1 </td> <td> 2 </td> </tr>';
- $('#contactInfo').delegate("button.addRow", "click" , function() {
- alert("button clicked");
- var row = $(this).closest(tr); // get the parent row of the clicked button
- $(html).insertAfter(row); // insert content
- });
});
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.