Answer : Use the input[type="file"]
element.
To use change event on input file/upload file using JQuery we can’t use the element id or class name.
Using $('input[type="file"]')
is to get the best result. See the following code :
Example :
1 | <input type="file"> |
1 2 3 4 5 | $(document).on("input", "input:file", function(e) { let fileName = e.target.files[0].name; alert('The file name is : "' + fileName); }); }); |
Another example :
1 | <input type="file"> |
1 2 3 4 5 | $('input[type="file"]').change(function(e) { let fileName = e.target.files[0].name; alert('The file name is : "' + fileName); }); }); |
See the following code in Playground :
Leave a Reply