.click function does not work on buttons that were generated dynamically by the .html function? [duplicate]

2

I have the following code:

var nReqAJAX = nReqAR
$.ajax({
    type: "POST",
    url: "../controller/ajax.selectItemRequisicaoPesquisar.php",
    data: {'numeroRequisicao': nReqAJAX},
    dataType: "JSON",
    success: function(res) {
        if(res == null)
        {
            $('#modalReqInvalida').modal('show');
        }
        else
        {
            var html = res.reduce(function(string, obj, i) {
                return string + '<tr class="text-center"><td style="vertical-align: middle;">' + i + '</td><td style="vertical-align: middle;">' + obj.nome_GAThemocomponente + '</td><td style="vertical-align: middle;">' + obj.qtd_GATitemRequisicao + '</td><td style="vertical-align: middle;">' + obj.frequencia_GATitemRequisicao + '</td><td style="vertical-align: middle;">' + obj.cirurgia_GATitemRequisicao + '</td><td><button id="1" name="btnAtender" class="btn btn-success" style="width: 100%;">Atender Item</button></td></tr>'
            }, '');
            $("#tab_logic tbody").html(html);
        }
    }
});

$("button[name='btnAtender']").on(function(){
    alert('teste');
});

A AJAX is executed when loading the page, if the return is null, I show a modal, if the result is not null, "draw" a table using the .html function, so far so good, the problem is that when I draw the table, I also draw a button in the last cell and say that the name of this button is btnAtender , however, I would like to do the following function:

$("button[name='btnAtender']").click(function(){
    alert('teste');
});

That is, when the user clicks on button btnAtender , a alert will appear showing text, however this simple function is not working, apparently because it is executed when from the click of a generated button dynamically, for test purposes, I created a <button name="btn">btn teste</button> in html page and the function worked.

What could I do with the alert function?

    
asked by anonymous 19.10.2017 / 16:53

4 answers

2

Do not do it this way:

$("button[name='btnAtender']").click(function(){
    alert('teste');
});

And this way:

$("button[name='btnAtender']").on('click', function(){
    alert('teste');
});
  

"This is basically how the association for the .click element is made.   applies to the current DOM, while .on (using delegation) will continue   for new elements added to the DOM after the association   of the event. "

Source: link

    
19.10.2017 / 17:37
1

No, because DOM has already been rendered, but you can solve by referring to document

$(document).on('click', '#gerar', function(){
   $('body').append('<button id="gerado">Executar Alert</button>')
})

$(document).on('click', '#gerado', function(){
    alert('oi')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><buttonid="gerar">Gerar</button>
    
19.10.2017 / 17:25
0

Set a promisse , stay like this ...

Set the click event as a function

function comp(){
    $("button[name='btnAtender']").on('click',function(){
        alert('teste');
    });
}

Placing the html

$("#tab_logic tbody").html(html).promise().done(function(){
    comp();
});

Your function will extend, so it will not be necessary to call the whole code again.

    
19.10.2017 / 17:35
0

I'll explain what happens. When you put this first code, the btnAtender button does not yet exist. There are several ways to work around this, but the one that would fix it quickly (and ugly) would be to put the code:

$("button[name='btnAtender']").on('click',function(){
    alert('teste');
});

just below this:

$("#tab_logic tbody").html(html);

This way the button will already be on the page when click is associated.

Try this:

var nReqAJAX = nReqAR
$.ajax({
    type: "POST",
    url: "../controller/ajax.selectItemRequisicaoPesquisar.php",
    data: {'numeroRequisicao': nReqAJAX},
    dataType: "JSON",
    success: function(res) {
        if(res == null)
        {
            $('#modalReqInvalida').modal('show');
        }
        else
        {
            var html = res.reduce(function(string, obj, i) {
                return string + '<tr class="text-center"><td style="vertical-align: middle;">' + i + '</td><td style="vertical-align: middle;">' + obj.nome_GAThemocomponente + '</td><td style="vertical-align: middle;">' + obj.qtd_GATitemRequisicao + '</td><td style="vertical-align: middle;">' + obj.frequencia_GATitemRequisicao + '</td><td style="vertical-align: middle;">' + obj.cirurgia_GATitemRequisicao + '</td><td><button id="1" name="btnAtender" class="btn btn-success" style="width: 100%;">Atender Item</button></td></tr>'
            }, '');
            $("#tab_logic tbody").html(html);
            $("button[name='btnAtender']").on(function(){
               alert('teste');
            });
        }
    }
});
    
19.10.2017 / 17:29