0

I have a page with two functions. Function A compiles an array and displays a button when done. The user clicks the button and the array is passed into Function B... All I have is Function A:

function createUploader(){
        var fileArray = new Array();
        var i = 0;
        var running = 0;
        var jList = $( "#list" );
            var uploader = new qq.FileUploader({
                element: document.getElementById('uploadDiv'),
                listElement: document.getElementById('separate-list'),
                action: './includes/ajaxUpload/upload.php',
        sizeLimit: 10485760,
        onSubmit: function(id, fileName){
            running++;  
        },
        onComplete: function(id, fileName, responseJSON){
            fileArray[i] = fileName;
            i++;
            running--;
            if(running==0){
            $('#combineBtn').css("display","");
            $.fancybox.resize();
            $('#fancybox-content').width(290);
            $('#fancybox-wrap').width(310);
            $.fancybox.center
            $('.qq-upload-button').width(290);
            }
        }
            });
        }

Is this even possible? What would be the best way to accomplish this?

3 Answers 3

4

Just declare the array outside of the functions, and it can be accessed by them both.

var myarray = [];
function foo(val) {
    myarray.push(val);
}

function bar() {
   alert (myarray);
}

Further reading: http://www.digital-web.com/articles/scope_in_javascript/

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

1 Comment

thanks, I wasn't sure if what was changed within the function would be global even though the var is global.
1

When creating the button in Function A, could you not do the following:

function function_a()
{
    var theArray = [1, 2, 3, 4];
    var theButton = $('<button>Click Me</button>');
    theButton.click(function() { function_b(theArray) });
}

function function_b(myArray)
{
    // Run function code here...
}

Comments

0

You can serialize array to json, save it in value attribute in hidden field, and when button is clicked read hidden field value in function B and deserialize JSON.

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.