Search code examples
jqueryjquery-file-uploadprop

Get jQuery property within a function


Just look at this example. Using jquery file upload

$('#button').fileupload({
    url: 'upload.app',
    dataType: 'json',
    autoUpload: false,
    sequentialUploads: true,

    add: function (e, data) {
    //access myOptions property here            
    },
    fail: function (e, data) {
    // and here
    },
    done: function (e, data) {
    // and here
    }
}).prop('myOptions',
        {
            optA: name,
            optB: 0                
        });

I wan't to access the myOptions property within the callback methods add,fail and done. But this.myOptions is only defined for the fail and done callback. Within the add callback this.myOptions is undefined. Is there any other way to access the myOption propety within the callback methods?


Solution

  • You can use .data() instead of .prop().

    $('#button').fileupload({
        url: 'upload.app',
        dataType: 'json',
        autoUpload: false,
        sequentialUploads: true,
    
        add: function (e, data) {
        //access myOptions property here  
             myOptions = $(this).data("myOptions")          
        },
        fail: function (e, data) {
        // and here
             myOptions = $(this).data("myOptions")
        },
        done: function (e, data) {
        // and here
             myOptions = $(this).data("myOptions")
        }
    }).data('myOptions',
            {
                optA: name,
                optB: 0                
            });