Feed Hidden field by clicking on a table column

Asked

Viewed 145 times

1

This is my jQuery that builds HTML into 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 click on a column (CNPJ or Razasocial), it feeds one or another Hidden field equivalent to column.

So I put the code passed to me and no Alert comes when I click on the corresponding td. tblGrid is the name of my table. I did so and nothing in 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
});
  • Click on a column or cell? What do you want as the value of the Hidden field?

  • 2

    Please avoid metamorphosing the question as you try the answers. In this case I suppose the very one #tblGrid is being dynamically inserted, no? Try $(document).on(... to confirm that this is the problem.

  • The following is supplementary to the question original with more information is highly desirable. Changing the question with the progress of its solution as you try the suggestions of the answers is not desirable, because the question becomes another. In this case, you could comment below my reply with something like: "I tested this code using #tblGrid as selector and Alert inside with value, but Alert does not appear".

  • Well, I saw that you accepted my answer. It worked now?

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 nearest ancestor that already exists at the time that this code will run):

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

    // e agora use o valor como quiser
});
  • It didn’t work. I believe I clicked wrong, IE, I may have marked another answer in another post and I ended up mistaken and clicked here, but did not solve no.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.