I'm not an expert on JS so I faced a problem in my plugin that I working on, the problem is I made a custom repeater field with uploader using wp.media for each field as you will see at the code below:
var mediaClientUploader;
// background
$(document).on('click', '.upload_client_logo', function(e) {
// blocking
e.preventDefault();
var $this = $(this);
var myInput = $this.prev('input');
//alert(myInput.data('repeat-count'));
if ( mediaClientUploader ) {
mediaClientUploader.open();
return;
}
mediaClientUploader = wp.media.frames.file_frame = wp.media({
title: 'Upload Picture',
button: {
text: 'Choose Picture'
},
multiple: false
});
console.log(mediaClientUploader);
mediaClientUploader.on('select', function () {
attachment = mediaClientUploader.state().get('selection').toJSON();
myInput.val(attachment.url);
});
mediaClientUploader.open();
});
The problem is this specific part
mediaClientUploader.on('select', function () {
attachment = mediaClientUploader.state().get('selection').toJSON();
myInput.val(attachment.url);
});
it's just running for one time, I mean with that is for the first select will append the URL over and over for the first input I choose!!
I know this problem I can solve it by $(document).on("select", "#div", function) but I don't know how I can use this approach with wp.media or my var mediaClientUploader
mediaClientUploader.on('select', function ()
, useinsert
instead ofselect
.