0

I took this sample piece of code from the below link

How do I close a dialog using jQuery?

$(function () {
    $("a:contains('sometext')").click(function() {
        var NewDialog = $('<div id="MenuDialog">\
            <p>This is your dialog content, which can be multiline and dynamic.</p>\
        </div>');
        NewDialog.dialog({
            modal: true,
            title: "title",
            show: 'clip',
            hide: 'clip',
            buttons: [
                {text: "Submit", click: function() {doSomething()}}
            ]
        });
        return false;
    });
});

Here instead of the value doSomething I need to use a variable. For eg:- if i have a variable temp=confirm, the value confirm should be used there. so when i clicked the button the function confirm should be invoked.

How can i achieve this?

2 Answers 2

1

If your confirm variable is a function you can just replace click function with confirm:

Edit:

 $(function () {
    var confirm1 = function(){
        alert("confirm function 1");
    }
    var confirm2 = function(){
        alert("confirm function 2");
    }

    var temp = confirm1;


    $("a:contains('sometext')").click(function() {
        var NewDialog = $('<div id="MenuDialog">\
            <p>This is your dialog content, which can be multiline and dynamic.</p>\
        </div>');
        NewDialog.dialog({
            modal: true,
            title: "title",
            show: 'clip',
            hide: 'clip',
            buttons: [
                {text: "Submit", click: function(){temp();}}
            ]
        });
        return false;
    });

    //  Uncomment this line if you want to change temp function
    //  temp = confirm2;

});

Fiddle: http://jsfiddle.net/nilgundag/SgFJk/4/

Sign up to request clarification or add additional context in comments.

1 Comment

since the functionname will vary i want to use a temp variable which will pass the functionname to the click event. So inshort i need to use a function name here.
0

I modified the code like below and it works..

buttons: [ {text: "Submit", click: function() {eval(temp)()}} ]

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.