13

This is a weird thing i noticed in chrome. if the user select a file and then select the same file again bu opening the file dialog again, chrome doesn't fire the onchange event while firefox does.

anybody noticed it as well?

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Amir
  • 1,219
  • 3
  • 17
  • 31

3 Answers3

5

This is a known feature of Chrome and a quick Google on the topic will bring up some interesting discussions.

It makes sense to me that the change event wouldn't fire since nothing has changed (you're selecting the same file)

As for your question, what exactly are you asking? Are you looking for a way to change this behaviour or do you just want to know if we've noticed this as well?

If you want a way around this behaviour you can simply create a new file input in your Javascript and replace the previous one. This way your change event is guarenteed to fire.

Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
  • 6
    actually i just set the input value to empty string and it reset it, so if the user will select the same file i will get the event. i have to say that i don't agree with you that this works as expected. I should get some notification that the user has selected a file (any file, even if it is the same one), but in this approach you (the progammer) don't know that this action occured. – Amir Jul 08 '11 at 12:07
3
function resetFileInput($element) {
  var clone = $element.clone();
  $element.replaceWith(clone);
}

And then use:

$('#element_id').on('change', function(){
...
});

Worked well for me!

Yevgen Safronov
  • 3,977
  • 1
  • 27
  • 38
oreoshake
  • 4,712
  • 1
  • 31
  • 38
2

An easier solution to reset an input file that works well for me :

document.getElementById('idOfInput').value = '';
tomahim
  • 1,298
  • 2
  • 11
  • 29
  • Note that this won't work in IE (IE8-IE11+) as file input is marked `readonly`. An alternative is to [wrap it in a form and reset the form](http://stackoverflow.com/a/14587296/1030702). – Bob Nov 26 '14 at 23:42