0

I want to get data from each row where the button is

$(document).ready(function() {
    listar_predios();



    function listar_predios() {
        $.ajax({
            type: 'ajax',
            url: $host + 'predios/listar_predios',
            asyn: true,
            dataType: 'json',
            success: function(data) {
                var html = '';
                var i;

                for (i = 0; i < data.length; i++) {
                    html += '<tr id="predios">' +
                        '<td>' + data[i].clave_catastral + '</td>' +
                        '<td>' + data[i].productor + '</td>' +
                        '<td>' + data[i].uso_suelo + '</td>' +
                        '<td>' + data[i].clase_tierra + '</td>' +
                        '<td>' + data[i].cultivo1 + '</td>' +
                        '<td>' + data[i].cultivo2 + '</td>' +
                        '<input class="clave_catastral" type="hidden" value="' + data[i].clave_catastral + '">' +
                        '<td style="text-align:right;">' +

                        '<button  class="btn btn-info btn-sm ver-mapa">VER EN MAPA</button>' +
                        '</td>' +
                        '</tr>';
                }

                $('#listado_predios').html(html);

                $('#tabla_predios').DataTable({
                    responsive: true,
                })


            }

        })
    }

});

Here I consolo log the value from the input, but just works on first row

$(document).ready(function() {

    $('#tabla_predios').on('click', 'button.ver-mapa', function() {
        clave = $('.clave_catastral').val();
        console.log(clave);
    });
Ceres
  • 3,524
  • 3
  • 18
  • 25
  • What data do you want to access when you click the button? Also, be careful about duplicating `id`s. You are creating multiple rows with the same id, "predios". – chazsolo Jun 07 '19 at 19:47
  • I want to get " data[i].clave_catastral ", yeah sorry about that, I just changed it to class, but I'm not using that identifier, I'm using the table itself – yheismx Jun 07 '19 at 19:56
  • Use $('.clave_catastral').each() See: https://stackoverflow.com/a/4735360/5514077 and https://api.jquery.com/each/ – TheWandererLee Jun 07 '19 at 20:02
  • If it's a simple value you could consider putting it as a data-* attribute on the button itself (so when you click the button it can grab the data from it's own `dataset`. – chazsolo Jun 07 '19 at 20:17

0 Answers0