Displaying File Names for Custom Styled File Inputs Using jQuery

Dec 03, 2025 · Programming · 11 views · 7.8

Keywords: jQuery | file upload | file name display

Abstract: This article provides an in-depth exploration of how to implement file name display functionality for custom-styled file input fields using jQuery. It begins by analyzing the original HTML and CSS structure, then delves into the mechanisms of jQuery's change event binding and extracting file names from the File API. By comparing multiple implementation approaches, including single-file and multi-file handling, as well as jQuery versus pure JavaScript methods, the article offers complete code examples and best practice recommendations to help developers elegantly manage file upload interfaces in front-end projects.

Introduction and Problem Context

In modern web development, file upload functionality is a core component of many applications. However, native HTML file input fields (<input type="file">) have significant limitations in terms of styling customization, prompting developers to adopt custom styling solutions. A common approach is to hide the native input and use a CSS-styled label (<label>) as a trigger element, thereby enhancing user experience. But this method introduces a new issue: users cannot visually see the name of the selected file after choosing it, which may lead to confusion or errors. Therefore, dynamically displaying the file name has become a key technical challenge in front-end development.

Technical Implementation Principles

The core solution to this problem lies in using JavaScript (particularly jQuery) to listen for change events on the file input field and extract file name information from the File API. When a user triggers the file selection dialog via the custom-styled label and completes the selection, the native input's change event is fired. By binding to this event, we can access the input's files property, which is a FileList object containing all files selected by the user. For single-file upload scenarios, typically only the first file (index 0) needs to be processed, and its name property provides the required file name.

Primary Implementation Solution

Based on the best answer (Answer 1, score 10.0), here is the standard method for implementing file name display using jQuery. First, ensure the HTML structure includes a hidden file input and a label serving as the trigger, for example:

<form>
  <label for="file-upload" class="custom-file-upload">
    <i class="fa fa-cloud-upload"></i> Upload Image
  </label>
  <input id="file-upload" name='upload_cont_img' type="file" style="display:none;">
</form>

The corresponding CSS styles the label for aesthetics:

.custom-file-upload {
  border: 1px solid #ccc;
  display: inline-block;
  padding: 6px 12px;
  cursor: pointer;
}

Next, use jQuery to bind the change event and update the label text to display the file name in the event handler:

$('#file-upload').change(function() {
  var fileName = $(this)[0].files[0].name;
  $(this).prev('label').text(fileName);
});

This code works as follows: $('#file-upload').change() listens for changes to the input; $(this)[0].files[0].name retrieves the name of the first file; $(this).prev('label').text(fileName) sets the file name as the text content of the preceding label. This method is straightforward, but note that if the original label contains icons or other elements, directly setting the text may overwrite them. An improved approach could involve adding a dedicated element for file name display, as shown in Answer 2.

Supplementary and Extended Solutions

Answer 2 (score 5.2) offers another common method by adding a separate element (e.g., <label id="file-name"></label>) to display the file name, avoiding interference with the original label's structure. Its jQuery code is:

$("#file-upload").change(function(){
  $("#file-name").text(this.files[0].name);
});

Additionally, Answer 2 demonstrates a pure JavaScript implementation, which is valuable for projects not relying on jQuery:

document.querySelector("#file-upload").onchange = function(){
  document.querySelector("#file-name").textContent = this.files[0].name;
}

Answer 3 (score 2.2) extends to multi-file upload scenarios by iterating through the files array to display all file names, for example:

updateList = function() {
  var input = document.getElementById('file');
  var output = document.getElementById('fileList');
  output.innerHTML = '<ul>';
  for (var i = 0; i < input.files.length; ++i) {
    output.innerHTML += '<li>' + input.files.item(i).name + '</li>';
  }
  output.innerHTML += '</ul>';
}

Although this method has a lower score, it is practical when multi-file selection support is needed.

Best Practices and Considerations

In actual development, consider the following best practices: First, always handle cases where file selection might be empty, such as by checking files.length to avoid errors; second, for user experience, add clear or reset functionality to the file name display area; third, ensure code compatibility—jQuery methods are often more concise, but pure JavaScript solutions may offer better performance. Additionally, be mindful of HTML escaping issues: when dynamically inserting file names, if they contain special characters (e.g., < or >), escape them to prevent XSS attacks, for example by using $.text() instead of $.html().

Conclusion

By combining jQuery's event handling with the File API, developers can easily add file name display functionality to custom-styled file input fields. This article starts from basic implementations, progressively delves deeper, covering single-file and multi-file scenarios, jQuery and pure JavaScript methods, and provides code examples and optimization suggestions. Mastering these techniques not only enhances user interface friendliness but also improves the overall functionality and security of applications. In the future, as web standards evolve, similar functionalities may become simpler, but these methods remain practical tools in front-end development today.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.