How can I select a row to add via ajax?

0

This is the code to select

$('#example tr').on('click', function () {
  var dato = $(this).find('td:nth-child(4)').html();
  alert(dato);
});

works well when the table is loaded with hard data, for example:

<table id="example>
  <thead>
    <tr>
      <th>Nombre</th>
      <th>Apellido</th>

    </tr>
  </thead>
  <tr>
    <td>Juan</td>
    <td>Perez</td>
  </tr>
</table>

but when adding rows by ajax does not work.

    
asked by Juan 01.06.2017 в 22:02
source

1 answer

0

do this:

$('#example').on('click', 'tr', function () {
  var dato = $(this).find('td:nth-child(4)').html();
  alert(dato);
});

As your table is dynamic, I'll explain why:

since the elements do not exist these do not obtain an event, since they do not exist. The best thing to do is to select an existing element and tell it that every element tr will get an event

    
answered by 01.06.2017 в 22:12