Bump
Multiple dropzone.js on one page - access "this"
Hi everyone,
Here is (probably) a nub javascript question:
I want to have multiple dropzones on the same page, so that the user can upload images in different locations on the page. Whenever the upload to the server is successful, the server sends back the new path of the uploaded image, which is then inserted into the closest div as a background image, or in the closest image instead of the src attribute.
It actually works alright, but the two dropzones are "linked together", so when I upload an image in one of them, it is also shown in the other location. How do I make sure that the image is only uploaded to the field where the dropzone was initialized from?
My JS:
var dropzone = $(".image-upload").dropzone({
url: "/upload-image",
maxFiles: 1,
paramName: 'photo',
addRemoveLinks: true,
sending: function(file, xhr, formData) {
formData.append('_token', $('meta[name="_token"]').attr('content'));
formData.append('name', 'header_image_path');
formData.append('pagecontent_id', $('body').data('page-id'));
},
init: function() {
this.on("success", function(file, response) {
var type = this.element.dataset['type'];
var imagePath = response.imagePath;
var parent = dropzone.parent();
// If the image type is a background image, set the background to the nearest parent
// Else we update the image path
if(type == 'background-image') {
parent.css('background-image', 'url(' + imagePath + ')');
} else {
parent.find('img').attr('src', imagePath);
}
});
},
uploadMultiple: false,
acceptedFiles: '.jpg, .jpeg, .png, .svg'
});
Html snippet (where type is background image):
<section class="intro-section" style="background-image: url({{ $content->header_image_path }})">
<div class="image-upload" data-type="background-image"></div>
Html snippet (where type is a regular image):
<div class="content-block-image">
<img src="{{ $content->content_block_two_image }}" alt="star-icon">
<div class="image-upload"></div>
</div>
Please or to participate in this conversation.