Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

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

  • Last Updated : 18 Aug, 2021

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: 
 

$(selector).change(function)
<input type="file"> 

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

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

html




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

html




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

 

jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :