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

Asked

Viewed 161 times

1

The title may seem a little confusing but what I want is basically this: on the button onclick pass to a function A a a function B as parameter. Only that already pass a parameter on which B will operate that in case is delete the product.

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

<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 exclusion it is already calling the excluirProduct() function. In the last line of the code snippet below you realize that the function passed via parameter should only be called if it is clicked on the ok button. But before that he already performs 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 believe it is because of the parentheses but I need to use them to pass the product.code parameter which is the product to be excluded.

So how can I pass the excluirProduct() function along with the parameter without running it?

  • Where it comes from produtos.codigo... vc is inserting a JS variable into an HTML element.

  • This is inside a each of javascript, I’m iterating in array of products to put them in a list. That’s all in javascript

  • Got it? Posted the answer and erased!

1 answer

0

I managed to solve my problem based on the answer 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 to that:

<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>

Workin' it’s a beauty.

Browser other questions tagged

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