3

Hey coders, i would like to initialize a dialog box with a callback function for say a 'save' button but i want the callback to reside as a standalone function rather than defined inline using function(){....} the code snippet below highlights what I want to do.

$( "#dialog-form" ).dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        buttons: {
            "Save": saveAction() 
...
function saveAction()  
{  
}  

what is the proper syntax for the "Save": saveAction() line cause it is doesn't seem to work?

thanks

3 Answers 3

4

The parens after saveAction makes the function execute. Use this instead:

        "Save": saveAction
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! thanks moff. what if I need to pass some parameters to the function? how can i do that?
If you need to pass parameters to that function you'll want to call it from an anonymous function e.g. "Save": function() { saveAction(a,b) })
0

saveAction must have parameters defined in the signature: i.e. saveAction(a,b,c), then when setting the callback do this:

"Save": saveAction({a = "val", b = "val", c = "val"})

1 Comment

getting syntax error. say i only need to pass one parameter: this is what I have: function saveAction(save_context){ dosomething }; so to set up the callback should i do this "Save": saveAction({context}) or "Save": saveAction({save_context = context})? thanks
0

If you have to pass in parameters, you must wrap your function call in an anonymous function definition, like this:

"Save": function() { saveAction({a = "val", b = "val", c = "val"}) }

This effectively defines a new anonymous function that takes no parameters, and which when executed will call your own function with your desired paramters.

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.