Is there a way in JS / React to execute code if input[type=”file”] has a file uploaded

i want to execute some code only if user has clicked on “add file” (when we use an input type file in html) and selected a file. Is there a way to do this ?


Yes, you can work with the input event like this:

const input = document.querySelector('input');

input.addEventListener('input', updateValue);

function updateValue(e) {
  log.textContent =;

You can of course modify this to suit your requirement. This example is from the Mozilla documentation here

Take a look at this answer which you may also find useful. It works with the change event.