How to fire an event on file select using jQuery ?
Given an HTML document and the task to fire an event when a file is selected using jQuery. The JQuery change() method is used to fire an event when file is selected.
Using change() method: It is used to change the value of the input fields. This method works with “input, textarea and select” elements.
Syntax:
$(selector).change(function)
Example 1: The change() method is used to fire an event when file is selected.
<!DOCTYPE html> <html> <head> <title> How to fire event on file select in jQuery? </title> <script src= </script> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <h3> How to fire event on file select in jQuery? </h3> <p>Click on button to select the file</p> <input type="file" id="Geeks" value="Click"> <script> $(document).ready(function() { $('input[type="file"]').change(function() { alert("A file has been selected."); }); }); </script> </body> </html> |
Output:

Example 2: The change() method is used to fire an event when file is selected.
<!DOCTYPE html> <html> <head> <title> How to fire event on file select in jQuery? </title> <script src= </script> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <h3>How to fire event on file select in jQuery?</h3> <p>Click on button to select the file</p> <input type="file" id="Geeks" value="Click"> <h4></h4> <script> $(document).ready(function(){ $('input[type="file"]').change(function(){ $("h4").text("File is added!"); }); }); </script> </body> </html> |
Output:

Recommended Posts:
- jQuery | select() with Examples
- How to select an element by name with jQuery?
- How to select elements by attribute in jQuery ?
- How to add options to a select element using jQuery?
- How to select all elements without a given class using jQuery ?
- How to select ID that starts with certain character using jQuery ?
- How to select all on focus in input using JQuery?
- How to select text nodes using jQuery ?
- How to remove options from select element using jQuery ?
- How to select first element in the drop-down list using jQuery ?
- jQuery UI | Spinner to select numeric data
- jQuery | event.which property with Examples
- jQuery | Selectors and Event Methods
- jQuery | event.namespace Property
- jQuery | event.data Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



