Pass function to another function as parameter with parameter in this function in javascript

0

The title may seem a bit confusing but what I want is basically this: in the onclick of the button pass to a function A a function B as a parameter.

I have a list of products each with a different code. I have the following button for each product that onclick has the function modalConfirmation () where I pass as parameter the title and message of the modal and the function to be executed that in the case is deleteProduct ():

<a onClick="script: modalConfirmacao('+"'Confirmar'"+','+"'Tem certeza que deseja excluir?'"+',excluirProduto('+produtos.codigo+'));"> <i class=" fa fa-check"></i>&nbsp;&nbsp;Excluir</a>

The problem is that without me confirming the deletion it is already calling the deleteProduct () function. In the last line of the code snippet below you realize that you should only call the function passed via parameter if the ok button is clicked. But before that it already executes the function.

function modalConfirmacao(titulo, mensagem,funcao) {
    var htmlModal = '<div class="modal fade" id="modalConfirmacao" tabindex="-1" role="dialog"  aria-hidden="true">' +
                        '<div class="modal-dialog">'+
                            '<div class="modal-content">'+
                                '<!-- Modal Header -->'+
                                '<div class="modal-header">'+
                                    '<h4 class="modal-title" id="myModalLabel">'+
                                        titulo
                                    +'</h4>'+
                                    '<button type="button" class="close" data-dismiss="modal">'+
                                        '&times;'+
                                    '</button>'+
                                '</div>'+
                                '<!-- Modal Body -->'+
                                '<div class="modal-body">'+
                                    mensagem
                                +'</div>'+
                                '<!-- Modal Footer -->'+
                                '<div class="modal-footer">'+
                                    '<button type="button" id="ok" class="btn btn-primary" data-dismiss="modal">'+
                                        'OK'+
                                    '</button>'+
                                    '<button type="button" id="cancelar" class="btn btn-default" data-dismiss="modal">'+
                                        'Cancelar'+
                                    '</button>'+
                                '</div>'+
                            '</div>'+
                        '</div>' +
                    '</div>';
            $('body').append(htmlModal);
            $("#modalConfirmacao").modal();
            $("#ok").off("click").on("click",funcao);
}

I think it's because of the parentheses but I need to use them to pass the parameter product.code that is the product to be excluded.

So how can I pass the DeleteProduct () function together with the parameter without executing it?

    
asked by anonymous 01.06.2018 / 05:43

1 answer

0

I was able to resolve my issue based on the response of this question :

In my case my link had this:

<a onClick="script: modalConfirmacao('+"'Confirmar'"+','+"'Tem certeza que deseja excluir?'"+',excluirProduto('+produtos.codigo+'));"> <i class=" fa fa-check"></i>&nbsp;&nbsp;Excluir</a>

And I've changed it:

<a onClick="script: modalConfirmacao('+"'Confirmar'"+','+"'Tem certeza que deseja excluir?'"+', jQuery.proxy(excluirProduto,this,'+produto.codigo+')));"> <i class=" fa fa-check"></i>&nbsp;&nbsp;Excluir</a>

Running that is a beauty.

    
01.06.2018 / 07:12