I use jQuery to work with dropzone. e.g.
$("#mydropzone").dropzone({ /*options*/ });I need to get the Dropzone instance so I can call this method:
myDropzone.processQueue()How can I achieve this? Is it possible?
in other words, how can I initialise drop zone using
$("#mydropzone").dropzone({ url: "/file/post" });but at the same time get the instance of the object as if i initialise it using:
var myDropzone = new Dropzone("#mydropzone", { url: "/file/post"});so I may call:
myDropzone.processQueue()Thank you very much.
15 Answers
As described in issue #180
You can also use the built in Dropzone.forElement function.
var myDropzone = Dropzone.forElement("#mydropzone"); 2 The script seems to add a dropzone object to the given element.
So you can do something like this :
var $dropZone = $("#mydropzone").dropzone({ /*options*/ });
// ...
$dropZone[0].dropzone.processQueue(); 4 Easy way to access the Instance with jQuery, if it has already been initialized:
var dropzone = $(this).get(0).dropzone; As stated before, you can use dropzone's forElement, which in turn just checks for element.dropzone, where 'element' is the native one (not jquery obj). So, to merge, summarize, explain and expand the previous answers, you can do like this:
var element = $("#mydropzone")[0]; // this is the way jquery gives you the original domor better, in just plain js:
var element = document.querySelector("#mydropzone");then get the dropzone like this:
element.dropzoneOr, more concise (plain js here):
var dzone = document.querySelector("#mydropzone").dropzoneJust for reference, the current dropzone's forElement source:
Dropzone.forElement = function (element) { if (typeof element === "string") { element = document.querySelector(element); } if ((element != null ? element.dropzone : undefined) == null) { throw new Error("No Dropzone found for given element. This is probably because you're trying to access it before Dropzone had the time to initialize. Use the `init` option to setup any additional observers on your Dropzone."); } return element.dropzone;
}; it's seem simple with Dropzone.instances check with id of element:
function fn_check_has_dropzone_instances(id){ var found = false; Dropzone.instances.forEach(function(item,index){ if($($(item)[0].element).attr('id').trim()==id.trim()){ console.log(id); found = true; } }); return found;
}