I am using window.FileReader to offer an instant preview of the images that the user chooses to upload.
I have created this jsFiddle
I have the following issues:
A) Code in line 63-77 seems to be ignored although when I print the value of "i" in line 25 I can see that is increasing (I am suspecting that it must have something to do with the nature of window.Filereader but not sure).
if (i == 0) //if this is the first picture add it as primary too
{
var primaryimage = ' <img id=' + filename + ' height="220" width="220" src=' + this.result + ' /> ';
$('#primary-pic').find('.custom-input-file').hide();
$('#primary-pic').find('p').hide();
$('#primary-pic').append(primaryimage);
$('.custom-input-file').show();
}
B) If you add more than one image and press "Set as default" for some reason this doesn't seem to work for the last image but no idea why .
Thanks
iin youronloadendhandler, which is the final valueigets by the time the function is invoked. In addition, you add the $(".setDefault:button") handler multiple times. I would use event delegation if I were you, so that the handler could be added only once to a surrounding element.