I'm loading in some data with Socket.IO and adding it to a table with a JS function inside the Socket.io listener. At the end of each row of that table, I'm trying to add a button that gets the content in the newly added row and does something with it. However, the event handler isn't firing for the newly added button. I feel that I am properly attaching on() to the parent and have tried every variation of this, but I still can't get the button to fire. Here's my code:
$('body').on('click', '.btn btn-warning', function() {
alert( $(this).text() );
});
socket.on('loadtable', function (table) {
$("#tableBody").append('<tr><td>' + table["name"] + '</td><td>' + table["id"] +
'</td><td>' + table["loc"] + '</td><td>' + table["area"] + '</td><td>' + table["zip"] +
'</td><td><a href="#editModal" role="button" class="btn btn-warning" data-toggle="modal"><i class="icon-edit icon-white"></i></a>'
+ '</td><td><input type = "checkbox"></td></tr>'
);
});
<table class="table table-bordered table-hover" id ="jobsTable">
<thead>
<tr>
<th class = "span2">Name</th>
<th class = "span2">Process</th>
<th class = "span2">JAR</th>
<th class = "span1">Main Class</th>
<th class = "span2">Args</th>
<th class = "span1">Edit</th>
<th class = "span1">Select</th>
</tr>
</thead>
<tbody id = "tableBody">
</tbody>
Am I overlooking something with the way the inside of a Socket function works?
try:
$('body').on('click', '.btn.btn-warning', function() {