javascript – Select name file then go to other lines?-ThrowExceptions

Exception or error:

I have this jsfiddle I want when the user clicks on “OK” first open browser file after user select file then continue other line. But my program before I select file go to other line. How can I do this?

popFileSelector = function() {
    var el = document.getElementById("fileElem");
    if (el) {
        el.click();  
    }
};

window.popWithDelay = function() {
        popFileSelector();
    document.getElementById('log').innerHTML += '1!<br />';
    document.getElementById('log').innerHTML += '2!<br />';
    document.getElementById('log').innerHTML += '3!<br />';
    document.getElementById('log').innerHTML += '4!<br />';

    document.getElementById('log').innerHTML += '5!<br />';
  
    
};
 
<body>
  <form>
      <input type="file" id="fileElem" multiple accept="image/*" style="display:none" onchange="handleFiles(this.files)" />
  </form>
     <br />
  <a href="javascript:popWithDelay()">OK</a>
    <div id="log">Log: <br /></div>
</body>
How to solve:

This code ensures that the other lines are only printed if the input actually changes.

jsfiddle.

window.popFileSelector = function() {
  var el = document.getElementById("fileElem");
  if (el) {
    el.click();
  }
};

window.popWithDelay = function(event) {
  handleFiles(event.target.files);

  document.getElementById('log').innerHTML += '1!<br />';
  document.getElementById('log').innerHTML += '2!<br />';
  document.getElementById('log').innerHTML += '3!<br />';
  document.getElementById('log').innerHTML += '4!<br />';

  document.getElementById('log').innerHTML += '5!<br />';
}

window.handleFiles = function(files) {
  document.getElementById('log').innerHTML += 'This is the handleFiles() function<br />';
}
<body>
  <form>
    <input type="file" id="fileElem" multiple accept="image/*" style="display:none" onchange="popWithDelay(event)" />
  </form>
  <br />
  <a href="javascript:popFileSelector()">Pop With 1 Second Delay</a>
  <div id="log">Log: <br /></div>
</body>

###

you can use callback function

jsfiddle

popFileSelector = function(callback) {
    var el = document.getElementById("fileElem");
    if (el) {
        el.click(); 
        el.addEventListener('change', callback);
    }
};


window.popWithDelay = function() {
    var func = function() {
      document.getElementById('log').innerHTML += '1!<br />';
      document.getElementById('log').innerHTML += '2!<br />';
      document.getElementById('log').innerHTML += '3!<br />';
      document.getElementById('log').innerHTML += '4!<br />';
      document.getElementById('log').innerHTML += '5!<br />';
    }

    popFileSelector(func);
};

Leave a Reply

Your email address will not be published. Required fields are marked *