0
<table>
    <tbody>
        <tr>
            <td>1</td>
            <td class="qty">1</td>
            <td class="price">2</td>
            <td class="disc">0</td>
            <td class="total">2</td>
        </tr>
        <tr>
            <td>2</td>
            <td class="qty">2</td>
            <td class="price">10</td>
            <td class="disc">10</td>
            <td class="total">18</td>
        </tr>
    </tbody>
</table>
$('#table').on('change', function () {
    var row = $(this).closest('tr');
    var total = 0;
    $('#tr', row).each(function() {
        total += Number($(this).val());
    });
    $('.total', row).text(total);
});

I am trying to get the code above to work. I am using the latest version of python. I need to listen to a table for a change so that I can get the last row. The row is added by jquery. The rows are added through out the day. I do not know when they will be added. I do not know how many rows will be added either. What libraries do I need to import to get this to work?

balderman
  • 22,927
  • 7
  • 34
  • 52
Hayden
  • 9
  • `$('#tr', row).each(function() { ... })` - Ids have to be **unique** – Andreas Aug 21 '21 at 16:23
  • Why is this tagged with `python`? – Andreas Aug 21 '21 at 16:24
  • Does this answer your question? [Event binding on dynamically created elements?](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Andreas Aug 21 '21 at 16:24
  • Why can't you use the code that adds the rows to do what you need? If you don't have access to that code then you can use a Mutation Observer to track rows – charlietfl Aug 21 '21 at 16:28
  • table and it inner elements (tr, td) does not have change events. use setInterval and compare current and previous value of $('#table').html() – Alexey Obukhov Aug 21 '21 at 16:37

0 Answers0