Search code examples
javascriptjquerynoty

noty, call a function on button click


This is a prototype function I use for displaying confirmation with buttons using noty.

function confirmation(message, call_func)
{
    var m=noty(
    {
        text: message,
        modal: true,
        layout : 'center',
        theme: 'notifications',
        buttons: [
        {
            addClass: 'general-button red', text: 'Yes', onClick: function($noty)
            {
                   call_func;
               $noty.close();
            }
        },
        {
            addClass: 'general-button', text: 'No', onClick: function($noty)
            {
                $noty.close();
            }
        }]
    });
    return m;
}

I am calling this function with the syntax,

confirmation("Are you sure want to delete this item?", "delete("+id+")");

So on clicking the Yes button, another function delete(id) have to be called. But it does not, why?

I checked with alert, alert(call_func). I alerts as delete(10) where 10 is ID at the instance.


Solution

  • Well here you are not calling the function

    call_func;  
    

    you are just referencing it

    And here you are just building a string

    "delete("+id+")")
    

    it is not a reference to a function call.


    What you need to do is pass in an actual function and execute the function.

    confirmation("Are you sure want to delete this item?", function(){ delete(id); });
    

    and

    call_func();