1

I have a dialog, in the dialog are dynamic buttons. In every button, a $.get() is generated.

The path to the file is partly variable, and the function (which will be executed on success) is variable too.

But I can't get it to work. The console says that the variables are undefined. I know why, because it's in a function. But I set the variables globally.

Can anybody explain me how I can get these variables to work?

I will provide my source code so you can see what I mean.


The source code

    var dialog_buttons = {};

    for(var i=0;i<socialMediaServ.length;i++)
    {
        dialog_buttons["Upload naar "+socialMediaServ[i]]= function()
        {
            $("#wait-dialog").dialog(
            {
                modal: true,
                resizable: false, 
                draggable: false,
                width:305,
                height:125,
                my:'center',
                at:'center',
                open: function(){}
            });

            $.get("../includes/social/"+socialMediaServ[i]+"/upload.php",{functie: "checkUser", fotonaam: fotoNaam}, window['users_'+socialMediaServ[i]+'_check'], 'json');
        }
    };
    dialog_buttons["Sluiten"]= function() 
    {
        $(this).dialog("close");
    };
    $("#dialog-foto").dialog("option", "buttons", dialog_buttons);
    $("#dialog-foto").dialog("open");

The variable socialMediaServ is set on the top of my page. socialMediaServ is an array with: Facebook,Twitter

Many thanks in advance!

1 Answer 1

2

I am sure I understand exactly what the problem is. I assume, that the socialMediaServ[i] value in the $.get is undefined.

I would try storing each array value in variable within the scope of the loop:

 for(var i=0;i<socialMediaServ.length;i++)
{
   (function(){
    dialog_buttons["Upload naar "+socialMediaServ[i]]= function()
    {
        $("#wait-dialog").dialog(
        {
            modal: true,
            resizable: false, 
            draggable: false,
            width:305,
            height:125,
            my:'center',
            at:'center',
            open: function(){}
        });

        $.get("../includes/social/"+socialMediaServ[i]+"/upload.php",{functie: "checkUser", fotonaam: fotoNaam}, window['users_'+socialMediaServ[i]+'_check'], 'json');
    }
  })(i);
};
Sign up to request clarification or add additional context in comments.

3 Comments

Yes that works, but he now defines twitter in ../includes/social/"+socialM+"/upload.php for both of the buttons (Facebook and Twitter)
Sorry about that. I fixed the code sample and added a closure to store the i reference.
Ah thanks, with this part var socialM = socialMediaServ[i]; in it just after (function(){ it's working great! Many thanks for your help!

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.