Using the deprecated .live()
method I am able to make this work exactly as I would like however it's not ideal to keep it this way. I've found similar questions suggesting to use the $(".update").on()
method or to call $(document).on('click', $('.update'), function()...)
however I wasn't able to make any of the solutions work. The best I was able to do was return the full table but never able to get my snap.key. Any suggestions to make this work with any besides .live()
would be very much appreciated.
<table id="itemTable">
<thead>
<tr>
<th>Item</th>
<th>Cost</th>
<th>Quantity</th>
<th></th>
</tr></thead>
<tbody id = "tableBody">
</tbody>
</table>
Firebase call:
db.on('child_added', snap => {
$('#tableBody').append('<tr><td class="up"><span>' + snap.key + '</span></td><td><button type = "button" class="update">Update</button></td></tr>');
});
button handler:
$(".update").live('click',function() {
var $row = $(this).closest("tr"); // Find the row
var $text = $row.find(".up").text(); // Find the text
console.log($text);
});