0

I want to create a dialog with function for button in this dialog by call a function. But it does not work.
My code below //Function initial dialog

function inti_dlg(selector, autoOpen, height, width, modal, num_button, fn_apply, fn_cancel, fn_close)
    {
        if (num_button>1)
        {
            selector.dialog({
                autoOpen: autoOpen,
                height:height,
                width:width,
                modal:modal,
                buttons:{
                    Apply:function(){fn_apply},
                    Cancel:function(){fn_cancel}
                },
                close:function(){fn_close}
            });
        }
        else{
            selector.dialog({
                autoOpen: autoOpen,
                height:height,
                width:width,
                modal:modal,
                buttons:{
                    Apply:function(){fn_apply}
                },
                close:function(){fn_close}
            });
        }
    }

//Function abc

function abc()
{
    alert("abc");
}

// Call initial dialog function

$(function (){
    inti_dlg($('#cde'), false, 440, 480, true, 1, abc(), '', abc());
    $('#clickhere').click(function(){$('#cde').dialog('open');});
});

Html :

<div id="clickhere">Click here</div>
<div id="cde">
     <div>Test : pass argument as a function</div>
</div>

2 Answers 2

2

http://devlicio.us/blogs/sergio_pereira/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx

Use

Function.apply()

Function.call()

to call the function passed as parameter. and you don't need to add paranthesis along with function name to pass as argument. just pass function name.

function inti_dlg(selector, autoOpen, height, width, modal, num_button, fn_apply, fn_cancel, fn_close)
{
    if (num_button>1)
    {
        selector.dialog({
            autoOpen: autoOpen,
            height:height,
            width:width,
            modal:modal,
            buttons:{
                Apply:function(){fn_apply.apply()},
                Cancel:function(){fn_cancel.apply()}
            },
            close:function(){fn_close.apply()}
        });
    }
    else{
        selector.dialog({
            autoOpen: autoOpen,
            height:height,
            width:width,
            modal:modal,
            buttons:{
                Apply:function(){fn_apply.apply()}
            },
            close:function(){fn_close.apply()}
        });
    }
}

and for calling this

$(function (){
inti_dlg($('#cde'), false, 440, 480, true, 1, abc, '', abc);
$('#clickhere').click(function(){$('#cde').dialog('open');});
});
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Riaz, I'm beginer, so hard to understand. Can you explain more for my case. Anyway i will try to improve my skill :)
:Thanks Riaz, it works well. Can i ask more : If i pass arguments to abc function => how to call abc function in inti_dlg
Do you have another way? Because inti_dlg is must static, when i need initial Dialog, i will call it.
I am afraid I couldn't get this. What you are trying to do. Needs little more explanation
I want to create a function that can dynamic initial dialog and pass functions to all button in this dialog
|
2

You are actually executing the function rather than passing it in by mistake. In order to pass in the function and make it very clear what is going on I suggest doing something like this:

var abc = function ()
{
    alert("abc");
}


$(function (){
    inti_dlg($('#cde'), false, 440, 480, true, 1, abc, '', abc);
    $('#clickhere').click(function(){$('#cde').dialog('open');});
});

Then when you are inside "inti_dlg" do not do anything to "fn_apply, fn_cancel or fn_close". You want to pass the function untouched into jQuery Dialog for execution there.

It can be tough to grasp at first but in order to be effective at JavaScript you might need to adapt your understanding of what a "function" is depending on what languages you are used to. Since JavaScript is a functional language (more or less) a function is a first class citizen and can be passed around just like any other variable like a string or an int and later executed by appending () to the function variable name (and passing in any arguments to the function)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.