Get element value after AJAX content is loaded

Asked

Viewed 98 times

0

Well, I’m having some doubts regarding the use of Ajax. I’m trying to get a value of a certain element <tr>. For that I put an attribute data- in <tr> which is dynamically generated by PHP, but always have the return of Undefined.

My call AJAX:

    $("#consulta").click(function (e) { 
        e.preventDefault();

        $.ajax({
            url: 'scripts/consulta.php',
            dataType: 'html',
            type: 'POST',
            success: function(data) {
                $("#conteudoConsulta").html(data);
                $("#modalConsulta").modal("show");
            }
        });     
    });

Shape I’m using to get the value:

    $("body").on("click", "tr", function () {
        $("tr").addClass("select");
        var id = $(this);
        alert(id.closest('tr').find('td').data('cod'));
    });

How could it be done in this situation?

Thank you in advance!

1 answer

2


If the data-cod is in the tr, then you should take the amount with:

HTML:

<tr data-cod="algum valor">

JS:

$("body").on("click", "tr", function () {
   $("tr").addClass("select");
   var id = $(this);  // elemento clicado
   alert(id.data('cod')); // exibe o data-cod do elemento
});
  • Thanks!! I checked the code later and noticed that it was my lack of attention. Taking advantage of the topic... There is a "better" way to do this operation?

  • For simplicity I think this is the best way.

  • If the answer solved, you can mark it as right. Abs!

Browser other questions tagged

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