Inputelement always the last on page

I’m trying to use widget to get the loop ID from the widget info, but no luck. Any ideas on how to do this?

var widget = [];
var classes = document.getElementsByClassName(‘uploadcare-uploader’);
if(classes) {
for (var ik = 0; ik < classes.length; ik++) {
// Works.
widget[ik] = uploadcare.Widget(classes[ik]);

  // Runs.
  widget[ik].onUploadComplete(function(info) {
    console.log(widget[ik]);
    var agent = widget.inputElement.getAttribute('agent');
    var savedFile = info.cdnUrl;
    window.livechatSettings.agents[agent].image = savedFile;
    document.querySelector('.sf-agent-image-'+agent).src = savedFile;
    saveJson();
  }),(ik);
}

}

Hi @jarkko,

Sorry for the delayed response!

I’m not quite sure what loop ID is referring to. Could you clarify? It seems you have more than one widgets on a page, and you need to listen for the onUploadComplete event of each one separately. In order to get all widget instances, use the following code:

const widgets = uploadcare.initialize(); // returns an array of widget instances

widgets.forEach(widget => {
  widget.onUploadComplete(fileInfo => {
    // do something
  });
});

Let me know if this doesn’t help.