I have a gallery in a page where you click plus and minus to add and remove gallery items to which you then add a file to the input for that newly appended item.
I have created a file reader to get the files url but it only works on the first element on that page, anything added after that dynamically is not affected.
here is my JS:
$('#gallery .gallery-item .file_upload').each(function() {
var $this = $( this );
//$('body').on('click', 'input', function() {
$this.on( 'change', 'input', function(evt){
var files = evt.target.files;
var file = files[0];
console.log(files[0]);
var reader = new FileReader();
reader.onload = (function(file) {
return function(e) {
console.log(e.target.result);
};
})(file);
reader.readAsDataURL(file);
});
});
The append gallery item function:
$('#gallery')
.on('cocoon:before-insert', function(e,image_field) {
image_field.insertAfter( $(this).next() );
})
.on('cocoon:after-remove', function(e, image_field) {
$(this).data('remove-timeout', 1000);
image_field.fadeOut('slow');
});
$('.gallery-item').each(function() {
$(this).next('a.add_fields').appendTo(this);
$(this).next('input').appendTo(this);
});
HTML:
<div class="image-field">
<div class="file_upload">
<input class="image_file" id="gallery_files" name="book[gallery_attributes][images_attributes][1412768497650][file]" type="file">
</div>
</div>
Can anyone tell me why the .each function will not work with this .on function for the newer items that are appended. I think it is the top most function thats obviously not working here and not the others below.