vincent05996
vincent05996

Reputation: 171

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 ?

Upvotes: 0

Views: 60

Answers (1)

Nick
Nick

Reputation: 3845

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

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

input.addEventListener('input', updateValue);

function updateValue(e) {
  log.textContent = e.target.value;
}

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.

Upvotes: 1

Related Questions