How to get file input by selected file name without path using jQuery ?

The task is to get the file input by selected filename without the path using jQuery. To select the file we will use HTML <input type=”file”>. After that we will get the file name by using the jQuery change() method. This method is used in the JQuery to get the file input by selected file name. And the HTML <input type=”file”> is used to specify the file select field and add a button to choose a file for upload to the form.

Syntax:

Below examples illustrates the approach:



Example 1: In this example we will display the file name with the extension by using the change() method and the file will be selected by the HTML <input type=”file”>.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to get the file input by selected file
        name without the path using jQuery?
    </title>
  
    <style>
        h1 {
            color: green;
        }
          
        body {
            text-align: center;
        }
          
        h4 {
            color: purple;
        }
    </style>
    <script src=
    </script>
</head>
  
<body>
    <h1
        GeeksforGeeks 
    </h1>
  
    <h3
        How to get the file input by selected<br>
        file name without the path using jQuery? 
    </h3>
  
    <input type="file" id="geeks">
    <h4><!-- Slected file will get here --></h4>
      
    <script>
        $(document).ready(function() {
            $('input[type="file"]').change(function(e) {
                var geekss = e.target.files[0].name;
                $("h4").text(geekss + ' is the selected file.');
  
            });
        });
    </script>
</body>
  
</html>    

chevron_right


Output:

Example 2: In this example we will display the filename with the extension through an alert, by using the change() method and the file will be selected by the HTML <input type=”file”>.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to get the file input by selected file
        name without the path using jQuery?
    </title>
  
    <style>
        h1 {
            color: green;
        }
          
        body {
            text-align: center;
        }
          
        h4 {
            color: purple;
        }
    </style>
    <script src=
    </script>
</head>
  
<body>
    <h1
        GeeksforGeeks 
    </h1>
  
    <h3
        How to get the file input by selected<br>
        file name without the path using jQuery? 
    </h3>
  
    <input type="file" id="geeks">
    <h4><!-- Slected file will get here --></h4>
      
    <script>
        $(document).ready(function(){
            $('input[type="file"]').change(function(e){
                var geekss = e.target.files[0].name;
                alert(geekss + ' is the selected file .');
            });
        });
    </script>
</body>
  
</html>    

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.