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 am using the plugin above. How in jQuery can I check if fileUpload is already applied?

Now I get the following error:

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 any way to check before function calls:

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

thanks

+7
source share
3 answers

You can add / set the class as your own flag. In this case, we will 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 below yoavmatchulsky , you can also do

 $('.upload:not(.applied)').addClass('applied').fileUploadUI({...}); 
+16
source

The jQuery file upload plugin stores a link to the FileUpload handler as a jQuery object . The error message "FileUpload with namespace" file_upload "already assigned to this element" comes from the plugin’s own check for this data link.

You can initialize the plugin as follows to prevent this error:

 $('.upload').not(function () { return $(this).data('file_upload'); }).fileUploadUI({/*...*/}); 
+2
source

You can also do it

 if(undefined != $('.upload').data('blueimp-fileupload') ){ console.log( 'plugin already applied to the element' ); } else console.log( 'plugin not applied to the element' ); 
+2
source

All Articles