:file (since jQuery 1.0) selects <input type="file"> elements — the upload control. Official docs: it is equivalent to [type="file"]. Prefer $("input:file") over bare $(":file").
Remember
$("input:file") // recommended
$("form input:file") // scoped to a form
$("input[type=file]") // same matches · faster (native CSS)
$(":file") // avoid — implies *:file
// Official idea: count file inputs in a form
$("input:file").length
.val() (clear only — browsers block setting paths)
Approach
How to Use :file
Prefix with input (or a form scope), then count, style, or listen for changes.
Prefer $("input:file") or $("#form input:file") over bare $(":file").
For modern speed, use $("input[type=file]") — same matches, native CSS.
Count with .length (official demo pattern).
Listen with .on("change", …) and read this.files.
Prevent accidental submits in demos with event.preventDefault() on the form.
Piece
Required?
Role
:file
Yes
Matches input of type file
Tag / form scope
Recommended
Avoids a universal * scan
Equivalent
—
[type="file"]
Return value
—
jQuery collection of matching file inputs
Pseudocode
Pseudocode
matches = []
for each element in search context:
if element is an INPUT
and element.type === "file":
matches.add(element)
return matches
// Same idea as CSS: input[type="file"]
Reference
Cheat Sheet
Keep these facts handy while selecting file inputs.
Topic
Detail
Syntax
$("input:file")
Since
jQuery 1.0
Equivalent
[type="file"]
CSS?
No — jQuery extension
Preferred CSS
$("input[type=file]")
Bare form
Avoid $(":file") → universal scan
Pick file
.on("change", …) + this.files
Security
Browsers block setting a file path via script
Try it
Live Preview
Count file inputs in a mixed form — same idea as the official demo.
Official pattern: report how many file inputs jQuery found.
Result
Ready — click Run to count file inputs.
Code
jQuery Examples
Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.
Example 1 — Official $("input:file") Count
Report how many file inputs are in the form — the official demo pattern.
jQuery
var n = $("input:file").length;
$("#out").text("For this type jQuery found " + n + ".");
$("form").on("submit", function (event) {
event.preventDefault();
});
Global.$("input:file") includes the other form too.
Habit. Always scope on multi-form pages.
Prefix. Still use input — never bare :file.
Example 4 — change and File Name
React when the user picks a file; read the name from files.
jQuery
$("input:file").on("change", function () {
var f = this.files && this.files[0];
$("#out").text(f ? "Selected: " + f.name : "No file");
});
$("#out").text("Choose a file…");
Check these before relying on :file in real forms.
bare
Bare $(":file") scans everything
Official docs recommend a tag prefix. Prefer $("input:file") or a form scope.
perf
Prefer [type="file"] for speed
:file cannot use querySelectorAll. Same matches via the attribute selector are faster.
security
Cannot set a file path in script
Browsers block assigning a path for security. You can clear with .val(""), but not pre-fill a file.
files
Use this.files, not only .val()
.val() may show a fake path. For name/size/type, read the FileList on the element.
Compatibility
Browser Support
The :file selector has been in jQuery since 1.0. It works wherever your jQuery build runs. Prefer input[type="file"] for modern performance.
✓ Stable · jQuery 1.0+
jQuery :file
Supported across jQuery 1.x, 2.x, and 3.x in every browser that runs your jQuery build. Prefer current jQuery 3.x and the CSS attribute form for new projects.
100%Form selector support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
:fileUniversal
Bottom line: Safe everywhere jQuery runs. Prefer input[type=file] for native speed, and always prefix with input.
Analysis
Performance Notes
Approach
Cost
Best for
$("input[type=file]")
Best
Modern browsers — native CSS
$("input:file")
Fine
Readable jQuery-style code
$(":file")
Slower
Avoid — universal scan
$("#form input:file")
Cheap
Multi-form pages
Because :file is a jQuery extension, prefer the attribute selector when performance matters. On small forms the difference is tiny — clarity and scoping matter more.
Remember
Key Takeaways
Role: match <input type="file"> — since jQuery 1.0.
Prefix: prefer $("input:file") over bare $(":file").
Speed::file ≡ [type=file] — prefer the attribute form.
Pick: use change + this.files for the selected file name.
One line::file finds upload inputs — prefix with input, prefer [type=file] for speed.
Frequently Asked Questions
:file selects every input element of type file — the control used to upload files. Prefer $("input:file"). Available since jQuery 1.0.
Yes. Official jQuery docs state that :file is equivalent to [type="file"]. For native querySelectorAll performance, prefer input[type="file"] over the :file pseudo-class.
Prefer $("input:file"). Bare $(":file") implies $("*:file") and scans every element type. Official docs recommend prefixing with a tag or other selector.
No. It is a jQuery extension since 1.0. Native querySelectorAll cannot run :file directly. Use [type="file"] for standard CSS attribute matching.
Select the control, then bind change: $("input:file").on("change", function () { var name = this.files[0] && this.files[0].name; }). File names come from the FileList on the input.
No. It only matches input elements whose type is file. Text, password, checkbox, and button inputs need their own type selectors or [type=…].