Crashdesk
Crashdesk

Reputation: 665

Pass variable from function to fancybox function

I have a variable being created in a function and I need to pass it into the fancybox onCleanup function for further processing in the parent page but the on onCleanup function is unable to get the variable. This is what I have at the moment...It can see the 'var fontSize = 'sdfs'" but not the variable created in the click function.

//Customise font size
$('#font-sizer a').on('click',function(){
    $('body').removeClass('font-size-small font-size-medium font-size-large font-size-very-large');
    $('#font-sizer a').removeClass('selected');
    switch($(this).attr("class"))
    {
    case "font-small":
        fontSize = "font-small";
        break;
    case "font-medium":
        fontSize = "font-medium";
        break;
    case "font-large":
        fontSize = "font-large";
        break;
    case "font-very-large":
        fontSize = "font-very-large";
        break;
    }
    $(this).addClass('selected');
    return false;
});
var fontSize = "sdfs";


    //Customise and Add Widgets Lightbox
    $("#customise,#add-widgets,#launch-profile,#view-all-apps").fancybox({
        'padding' : 0,
        'width'             : 940,
        'height'            : 634,
        'autoScale'         : false,
        'transitionIn'      : 'none',
        'transitionOut'     : 'none',
        'type'              : 'iframe',
        'scrolling'     : 'no',
        'onCleanup' : function() {
            window.parent.ifDoneChildFrame(fontSize);
        }
    });

Upvotes: 0

Views: 1097

Answers (1)

AlexKempton
AlexKempton

Reputation: 1698

If using a global variable is what you want to do, all you should need to do is place:

var fontSize;

...at the top of your script, just inside:

$(document).ready(function(){

You will have to remove the other declaration:

var fontSize = "sdfs";

By declaring the fontSize variable outside of a specific function, even without giving it a value, it is then accessible by all functions. This can be problematic of course, if the variable needs to be used in different ways by different functions.

More info on scope of variables here: http://www.w3schools.com/js/js_variables.asp

Upvotes: 1

Related Questions