1

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

1
  • on mediaClientUploader.on('select', function () , use insert instead of select.
    – mghhgm
    Feb 15, 2019 at 22:58

1 Answer 1

0

I have found the problem :"D it was this part of the code

 if ( mediaClientUploader ) {
    mediaClientUploader.open();

    return;
}

it stops my select event functionality after the first click so I just delete it and everything is fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.