Feed hidden field by click on the column of a table

1

This is my jQuery that mounts HTML in my cshtml.

$(data.resultado).each(function () {
                str += '<tbody>';
                    str += '<tr>';
                        str += '<td>';
                        str += '<label style="font-size:10px"><a href="#">' + data.resultado[cont].CNPJ + '</a></label>';
                        str += '</td>';
                        str += '<td>';
                        str += '<label style="font-size:10px"><a href="#">' + data.resultado[cont].RazaoSocial + '</a></label>';
                        str += '</td>';
                    str += '</tr>';
                str += '</tbody>';

                cont++;
            });

How do I do when clicking on a column (CNPJ or RazaSocial), it feeds one or another hidden field equivalent to the column.

So, I put the code passed to me and there is no alert when I click on the corresponding td. tblGrid is the name of my table. I did so and nothing on the alert

$('#tblGrid').on('click', 'td', function (e) {
    var ancora = $(this).find('label a');
    var valor = ancora.text();

    alert(valor);

    // e agora use o valor como quiser
});
    
asked by anonymous 08.05.2014 / 20:32

1 answer

3

Since you are injecting this HTML into the document, it is safer to use delegation (replace the selector I used below with something that selects the table, or the ancestor closest to it that already exists at the time that code is going to run):

$('table').on('click', 'td', function(e) {
    var ancora = $(this).find('label a');
    var valor = ancora.text();

    // e agora use o valor como quiser
});
    
08.05.2014 / 20:44