Skip to content
Related Articles

Related Articles

How to get file input by selected file name without path using jQuery ?
  • Last Updated : 28 Feb, 2020

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”>.




<!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>    

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”>.




<!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>    

Output:

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :