I have bound two separate events for testing, but for whatever reason neither of them are triggering when I select a file. I'm sure I have just not had enough coffee today but it seems to be the correct way of detecting file selection.
HTML:
<input type="file" id="files">
JS:
$(document).on("ready", function() {
$("#files").on("change", function() {
alert("Files changed.");
});
$(document).on("change", "#files", function() {
alert("Files changed.");
});
});
example: /
I have bound two separate events for testing, but for whatever reason neither of them are triggering when I select a file. I'm sure I have just not had enough coffee today but it seems to be the correct way of detecting file selection.
HTML:
<input type="file" id="files">
JS:
$(document).on("ready", function() {
$("#files").on("change", function() {
alert("Files changed.");
});
$(document).on("change", "#files", function() {
alert("Files changed.");
});
});
example: https://jsfiddle/snoapps/66y45hjh/
Share Improve this question edited Nov 30, 2021 at 20:03 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 19, 2016 at 16:12 Micaiah WallaceMicaiah Wallace 1,14610 silver badges17 bronze badges2 Answers
Reset to default 6https://jsfiddle/66y45hjh/2/
Your second function was correct but the way the functions are nested wouldn't let it execute the way you wanted it to. Take a look at the edit I made to your fiddle.
The javascript code I used was as follows.
$(function() {
$(document).on("change", "#files", function() {
alert("Files changed.");
});
});
It's
$('document').ready(function(){});
https://api.jquery./readyYou don't need your second function
So:
$(document).ready(function() {
$("#files").on("change", function() {
alert("Files changed.");
});
});