Call Javascript function with onclick with content loaded div

2

I have a div that has its content loaded after loading the rest of the page. It is called by a button via AJAX. I have another button inside this content that would call a Javascript function to confirm a page targeting. But when it's inside the loaded content the button does not work.

Follow the function code.

<script language="Javascript">

function confirmacao(aluno) {
     var resposta = confirm("Deseja remover esse aluno?");

     if (resposta == true) {
          window.location.href = "del_aluno_done.php?aluno="+aluno;
     }
}
</script>
                    
asked by anonymous 28.04.2014 / 14:31

1 answer

2

Your code has some errors. I do not know if it was a hurry to put / simplify the example, or if you have errors in your code.

Once you've added the jQuery tag here is a suggestion by removing all JavaScript from your HTML.

1 - Give the button a class 2 - If it is called a button, it might be a good idea to use <button> instead of <a> mainly because there is no link to click, only javascript behavior that should be run ...
3 - The information, possibly passed through PHP, that refers to this element can be stored in a data-

So instead of

<a href='javascript:func()'onclick='confirmacao('$aluno;)'>

You can use

<button type="button" data-aluno="<?php echo $aluno; ?>" class="botaoAluno">Clique aqui</button>

And in Javascript / jQuery

$(document).ready(function(){
    $(document).on('click', '.botaoAluno', function(){
        func();
        var aluno = $(this).data('aluno');
        var resposta = confirm("Deseja remover esse aluno?");

         if (resposta == true) {
              window.location.href = "del_aluno_done.php?aluno=" + aluno;
         }
         // ou pode manter a funcao à parte e chamar 'confirmacao(aluno);' aqui dentro
    });
});
                                    
28.04.2014 / 15:11