Adding a new table function works and adding a row function works on tables that were already created on the page. However, when I try adding a row to a table that I added dynamically it does not work.
The alert for testing when I click add row does not do anything.
<script type="text/javascript>
$(document).ready(function() {
$(".addtable").on("click", function () {
$(".order-list:last").each(function() {
closesttable = $(this).closest('table').attr('id');
tableid = closesttable.replace(/^.+-/,'');
});
tableid=parseInt(tableid)+1;
var newTable = $("<div class='tableDemo'>");
var cols = "<table id='table-"+tableid+"' class='order-list'>";
cols += '<tbody><tr id="1"><td>'+tableid+'</td>';
cols += '<td>1</td>';
cols += '<td>1</td>';
cols += '<td><input type="text" name="Quantity['+tableid+'][1]" value=""/></td>';
cols += '<td><select name="Meal['+tableid+'][1]"><option></option><option>Chicken</option><option>Fish</option><option>Bison</option></select></td>';
cols += "<td><input type='submit' name='Delete["+tableid+"][1]' value='Delete' id='ibtnDel' style='height: 20px;'><a class='deleteRow'></a></td></tr>";
cols += "</tbody><tfoot><tr id='addrow1'><td colspan=5><input type='button' class='addrow' value='Add Row' style='height: 20px;'></td></tr></tfoot></table></div>";
newTable.append(cols);
$("#here_table").append(newTable);
$(".addrow").on("click", function () {
alert("new addrow clicked");
});
});
});
<div id="here_table"></div>
<BR><BR>
<input type='button' class='addtable' value='Add Table'>
Uncaught ReferenceError: tableid is not defined$(".addtable").on("click", function () {to the dynamic use ofonor.delegate, like:$(document).on('click', '.addtable', function(e) {OR$(document).delegate('.addtable', 'click', function(e) {