How to get attributes from fineuploader handler?
-
21-12-2019 - |
Question
I have the following piece of code
<div class="cv-uploader" id="customid">Upload CV</div>
$('.cv-uploader').fineUploader({
debug: true,
request: {
endpoint: '/process',
params:{ elementId:$(this).attr('id') }
},
//some other code
});
I need to send in the request parameters the ID of the element that triggered fineuploader. I tried $(this).attr('id') but it wont work.
Please help
Thanks Sergiu
Solution
This is more of a general JavaScript question then a Fine Uploader question. The problem is that you are misunderstanding how context works in JavaScript. In your code, this
will always refer to the window
object. Obviously, this is not what you want.
You can either do this:
$('.cv-uploader').fineUploader({
debug: true,
request: {
endpoint: '/process',
params:{ elementId:$('.cv-uploader').attr('id') }
},
//some other code
});
...or this:
$('.cv-uploader').fineUploader({
debug: true,
request: {
endpoint: '/process'
},
//some other code
})
.on("submitted", function(event, id) {
$(this).fineUploader('setParams', {elementId: $(this).attr('id')}, id);
});
The latter example is possible as Fine Uploader's jQuery plug-in wrapper sets the context of any jQuery event handlers you bind to a Fine Uploader instance as the jQuery object that represents that element.
However, I would recommend the first approach as it is more efficient.