Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery, how to check if a plugin has already been applied to a div?

jQuery File Uploader: https://github.com/blueimp/jQuery-File-Upload

I'm using the plugin above. How in jQuery can I check to see if the fileUpload has already been applied?

I get the following error now:

Uncaught FileUpload with namespace "file_upload" already assigned to this element
jQuery.jQuery.extend._Deferred.deferred.resolveWithjquery-1.5.1.js:869
donejquery-1.5.1.js:6591
jQuery.ajaxTransport.send.callbackjquery-1.5.1.js:7382

Is there a way to check before my function calls:

$('.upload').fileUploadUI({
 .........
 .........
 .........

Thanks

like image 205
AnApprentice Avatar asked Apr 14 '11 21:04

AnApprentice


1 Answers

You can add/set a class as a flag of sorts. In this case, we'll add a class called applied

//scroll through each upload item
$('.upload').each(function(){

    //Make sure class is not found
    if (!$(this).hasClass('applied')) 

        //Apply Class and Upload Plugin
        $(this).addClass('applied').fileUploadUI({...}); 
}); 

Update as pointed out below by yoavmatchulsky, you could also, more easily do

$('.upload:not(.applied)').addClass('applied').fileUploadUI({...});
like image 187
Dutchie432 Avatar answered Sep 27 '22 16:09

Dutchie432