If you can use ES6 syntax i would call on success your function as an arrow function so this
will be inherited from the parent scope:
$('.dropzone').each(function(index){
$maxfiles = $(this).attr('maxfiles');
$(this).dropzone({
paramName: 'postedFile',
addRemoveLinks: true,
dictDefaultMessage: 'Sleep je bestand(en)',
dictRemoveFile: 'Verwijder',
dictCancelUpload: 'Annuleren',
dictInvalidFileType: 'Dit type bestand is niet toegestaan',
dictCancelUploadConfirmation: 'Weet je zeker dat je het uploaden wilt annuleren?',
dictMaxFilesExceeded: 'Maximale aantal bestanden overschreden',
maxFiles: $maxfiles,
acceptedFiles: '.jpg, .jpeg, .png, .pdf, .tif, .tiff',
thumbnailWidth: '150',
thumbnailHeight: '120',
thumbnailMethod: 'crop',
previewTemplate: $(".hiddendiv").html(),
// File contains dropzone file object, response contains ajax response from php file
success: (file, response) => {
var obj = JSON.parse(response);
console.log(file);
if(obj[0].status == 'success'){
$(this).find('.fas').removeClass('afgekeurd').addClass('goedgekeurd');
$(this).find('.fas').removeClass('fa-times-circle').addClass('fa-check-circle');
}else if(obj[0].status == 'error'){
$(this).find('.fas').removeClass('fa-check-circle').addClass('fa-times-circle');
$(this).find('.fas').removeClass('goedgekeurd').addClass('afgekeurd');
$(this).find('.vrijgevenbtn').show();
}
$(this).find('.resolutie').text('Resolutie: ' + obj[0].dpi + ' DPI');
$(this).find('.formaat').text('Formaat: ' + obj[0].heightcm + ' x ' + obj[0].widthcm + 'cm');
},
})
});
I am using Dropzonejs to create an upload tool. The problem is I have a custom previewtemplate, where some information is displayed of the uploaded image (DPI, dimensions etc). On success
return I update the info of that previewtemplate, but when I upload a new image, all previewtemplates are updated with the info of the last uploaded image instead of just that one.
This is my javascript:
$('.dropzone').each(function(index){
$maxfiles = $(this).attr('maxfiles');
$thisdropzone = $(this); // Set $(this) to use later on
$(this).dropzone({
paramName: 'postedFile',
addRemoveLinks: true,
dictDefaultMessage: 'Sleep je bestand(en)',
dictRemoveFile: 'Verwijder',
dictCancelUpload: 'Annuleren',
dictInvalidFileType: 'Dit type bestand is niet toegestaan',
dictCancelUploadConfirmation: 'Weet je zeker dat je het uploaden wilt annuleren?',
dictMaxFilesExceeded: 'Maximale aantal bestanden overschreden',
maxFiles: $maxfiles,
acceptedFiles: '.jpg, .jpeg, .png, .pdf, .tif, .tiff',
thumbnailWidth: '150',
thumbnailHeight: '120',
thumbnailMethod: 'crop',
previewTemplate: $(".hiddendiv").html(),
// File contains dropzone file object, response contains ajax response from php file
success: function (file, response) {
var obj = JSON.parse(response);
console.log(file);
if(obj[0].status == 'success'){
$($thisdropzone).find('.fas').removeClass('afgekeurd').addClass('goedgekeurd');
$($thisdropzone).find('.fas').removeClass('fa-times-circle').addClass('fa-check-circle');
}else if(obj[0].status == 'error'){
$($thisdropzone).find('.fas').removeClass('fa-check-circle').addClass('fa-times-circle');
$($thisdropzone).find('.fas').removeClass('goedgekeurd').addClass('afgekeurd');
$($thisdropzone).find('.vrijgevenbtn').show();
}
$($thisdropzone).find('.resolutie').text('Resolutie: ' + obj[0].dpi + ' DPI');
$($thisdropzone).find('.formaat').text('Formaat: ' + obj[0].heightcm + ' x ' + obj[0].widthcm + 'cm');
},
})
});
This line:
$thisdropzone = $(this); // Set $(this) to use later on
doesn’t work because $(this)
works down the element tree of the entire dropzone element, not the previewtemplate which is what I need.
How can I only update info for each individual previewtemplate?
This is the html of one previewtemplate which is added inside the dropzone element when I upload an image:
<form action="upload/uploaden.php" class="dropzone dz-clickable dz-started" maxfiles="10" id="dropzone1">
<input type="hidden" value="Monomeer" name="productnaam">
<input type="hidden" value="Twan" name="klantnaam">
<input type="hidden" value="20" name="hoogte">
<input type="hidden" value="20" name="breedte">
<div class="dz-default dz-message"> <span>Sleep je bestand(en)</span> </div>
<div class="dz-preview dz-processing dz-image-preview dz-complete">
<div class="dz-image"> <img data-dz-thumbnail="" alt="Untitled-4.jpg" src=""> </div>
<div class="dz-details">
<div class="dz-size"> <span data-dz-size="">
<strong>0.1</strong> MB
</span> </div>
<div class="dz-filename"> <span data-dz-name="">Untitled-4.jpg</span> </div>
</div>
<div class="dz-progress"> <span class="dz-upload" data-dz-uploadprogress="" style="width: 100%;"></span> </div>
<div class="dz-error-message"> <span data-dz-errormessage=""></span> </div> <span class="toewijzen">Aantal toewijzen</span>
<div class="uploadcontent">
<input type="text" class="fileinput">
<button class="plusminupload" id="minupload">−</button>
<button class="plusminupload" id="plusupload">+</button>
</div>
<hr class="uploadline"> <span class="infoline">
<span class="infospan resolutie">Resolutie: 72 DPI</span> <i class="fas goedgekeurd fa-check-circle"></i> </span> <span class="infoline">
<span class="infospan formaat">Formaat: 20,00 x 20,00cm</span> <i class="fas goedgekeurd fa-check-circle"></i> </span>
<button class="yellowbtn btn vrijgevenbtn" type="button" style="display: inline-block;">Bestand vrijgeven</button> <a class="dz-remove" href="javascript:undefined;" data-dz-remove="">Verwijder</a> </div>
<div class="dz-preview dz-processing dz-image-preview dz-complete">
<div class="dz-image"> <img data-dz-thumbnail="" alt="20x20cm.jpg" src=""> </div>
<div class="dz-details">
<div class="dz-size"> <span data-dz-size="">
<strong>0.2</strong> MB
</span> </div>
<div class="dz-filename"> <span data-dz-name="">20x20cm.jpg</span> </div>
</div>
<div class="dz-progress"> <span class="dz-upload" data-dz-uploadprogress="" style="width: 100%;"></span> </div>
<div class="dz-error-message"> <span data-dz-errormessage=""></span> </div> <span class="toewijzen">Aantal toewijzen</span>
<div class="uploadcontent">
<input type="text" class="fileinput">
<button class="plusminupload" id="minupload">−</button>
<button class="plusminupload" id="plusupload">+</button>
</div>
<hr class="uploadline"> <span class="infoline">
<span class="infospan resolutie">Resolutie: 72 DPI</span> <i class="fas fa-check-circle goedgekeurd"></i> </span> <span class="infoline">
<span class="infospan formaat">Formaat: 20,00 x 20,00cm</span> <i class="fas goedgekeurd fa-check-circle"></i> </span>
<button class="yellowbtn btn vrijgevenbtn" type="button">Bestand vrijgeven</button> <a class="dz-remove" href="javascript:undefined;" data-dz-remove="">Verwijder</a> </div>
</form>
In this example there are multiple .dz-preview
(the class of each previewtemplate) classes like you see. How can I use jQuery $(this)
on that element?
Thanks but this still has the same result. All previewtemplates get updated with the latest image info.
That’s because this is referred to your whole form and jQuery find() Is returning all elements with resolutie and formaat classes inside the form
You can use multiple forms for example or try to target only the span you want to edit