I am trying to invent hot water :D. I am using html5Uploader and everything is working fine (images are being uploaded without any error).
Problem comes a bit latter, in this part:
onServerLoad: function(e, file) {
$('.img_upload').on('click', function() {
$('#text').val($('#text').val() + $(this).attr('title'));
});
}
Value is being added to the textarea but it is added number of times as there are uploaded images (for example I have 5 images with name first.png it will add first.png five time to the text area ). How can I avoid this?
$(function() {
var fileTemplate = "<div id=\"{{id}}\">";
fileTemplate += "<div class=\"preview\"></div>";
fileTemplate += "<div class=\"filename\">{{filename}}</div>";
fileTemplate += "</div>";
function slugify(text) {
text = text.replace(/[^-a-zA-Z0-9,&\s]+/ig, '');
text = text.replace(/-/gi, "_");
text = text.replace(/\s/gi, "-");
return text;
}
$("#dropbox").html5Uploader({
onClientLoadStart: function(e, file) {
var upload = $("#upload");
if (upload.is(":hidden")) {
upload.show();
}
upload.append(fileTemplate.replace(/{{id}}/g, slugify(file.name)).replace(/{{filename}}/g, file.name));
},
onClientLoad: function(e, file) {
$("#" + slugify(file.name)).find(".preview").append("<img class=img_upload title=\"" + file.name + "\" src=\"" + e.target.result + "\" alt=\"\">");
},
onServerLoad: function(e, file) {
$('.img_upload').on('click', function() {
$('#text').val($('#text').val() + $(this).attr('title'));
});
}
});
});
One way would be to check to see if it’s already in there, like this:
But I think the problem may be in the way you’re fetching the name, try using e.target.result or file.name
If you want to add the same one a specific number of times, there are a few ways to do that.
Via for loop:
Otherwise, you can check the number of occurrences, and then append if necessary:
I’ve tried making the comments are informative as possible, but if you do not understand a part of it, just comment below. Try it out and tell me how it goes! XD