jQuery Custom File Upload Input: from the book Designing with Progressive Enhancement
April 2020 note: Hi! Just a quick note to say that this post is pretty old, and might contain outdated advice or links. We're keeping it online, but recommend that you check newer posts to see if there's a better approach.
As websites expand content-sharing and collaboration features, users are
electing to upload more and more files—photos, videos, documents, even
secure data—via web applications to store them in “cloud”-based systems
rather than locally on their computers. The HTML
input element with a
type="file" attribute gives web developers a native control to handle
this file upload functionality. But browsers offer almost no control
over how file inputs are presented visually, making it difficult to
incorporate them into a uniform interface design.
Today, we’re releasing a jQuery file input plugin that uses progressive
enhancement to transform a standard HTML file input into a visually
widget allows you to create a custom-styled file input that leverages
all of the accessibility features the native
This widget is one of the 12 fully-accessible, project-ready, progressive enhancement-driven widgets we created to accompany our book, Designing with Progressive Enhancement.
A quick demoPermalink to 'A quick demo'
Before we get into the mechanics of the custom-styled input control, here’s a demo that highlights key features. The input box and buttons have distinct, custom visual style; and when the user selects a file, the custom input updates to display the file name and an icon representing the file’s extension.
How does this widget actually work?Permalink to 'How does this widget actually work?'
The approach we’ve used piggybacks on top of the native file
The widget creates a custom-styled file control using
to zero and dynamically position it invisibly between the cursor and the
custom control. The user actually interacts only with the native
without knowing it. The following infographic demonstrates the concept:
In addition, the widget script applies custom styling, and parses the filename’s extension to recognize the file format so it can display a file-specific icon (the CSS includes a generic file icon and several specific icons—image, multimedia, zip folder—which are easily customizable).
The best part of this approach is that we get great accessibility
support for keyboard users and those with disabilities automatically.
Since we’re always using the native file input control, we can leverage
all the functionality and accessibility that it provides—the custom file
input is really a visual feedback mechanism only. And though the native
input is invisible, it’s still fully keyboard-accessible, so there’s
no need to add any additional accessibility features or ARIA attributes.
(We do, however, add one ARIA attribute to the custom widget to hide it
from screen readers, since it’s relevant only to sighted users.)
A tip of the hatPermalink to 'A tip of the hat'
How do I use it?Permalink to 'How do I use it?'
To use this script in your page, you’ll need to download and reference
both jQuery and jQuery.fileinput.js, as well as the associated CSS and
images for the control (download instructions are found below), and call
customFileInput method on any list element on the page. For
example, to create a custom input control from a
input element with an
file, find it using jQuery and call the
method on it:
<input type="file" name="file" id="file" />
Note: be sure to use jQuery’s
$(document).ready event to wait
until the DOM is ready before running the script above.
Also, you can disable and enable the control by calling either
the native input. Once disabled, the custom control will be styled to
appear disabled as well. If the input is disabled at load, the custom
control will naturally inherit that state, so you won’t need to call the
“disable” method when you first create the control.
That’s all there is to it!
If you’d like to learn more, we explain in detail how this widget works under the covers and how to apply these principles in different interactions or more complex scenarios in our book, Designing with Progressive Enhancement.