Forum Moderators: open

Message Too Old, No Replies

Firefox fires on choose, not on change

         

Rain_Lover

1:57 pm on Dec 14, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Consider this:

<input type="file" id="filePicker">
<script>
document.getElementById('filePicker').onchange = function() {
alert('Hi!');
};
</script>


Even if you choose the same file and the filePicker value doesn't change, you'll see the alert box in Firefox. Any solutions?

Fotiman

3:45 pm on Dec 14, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



One option might be to store the previous value in a variable and compare it to see if the value really changed. Something like:

var filePicker = document.getElementById('filePicker'),
prevValue = filePicker.value;

filePicker.onchange = function(e) {
if (prevValue !== this.value) {
// true change event
prevValue = this.value;
alert('Hi!');
}
};

Fotiman

3:47 pm on Dec 14, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Side note:
[developer.mozilla.org...]

Different browsers do not always agree whether a change event should be fired for certain types of interaction.

Rain_Lover

4:35 pm on Dec 14, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Perfect! But I wonder if it's necessary to add the e parameter to the function.

Fotiman

5:25 pm on Dec 14, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No, it's not. Just remove it. :)