jQuery :file Selector

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is :file?

: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
NeedUse
Find file inputs$("input:file") · prefer $("input[type=file]")
All form controls$(":input")
Text fields$("input:text")
Password fields$("input:password")
Read / clear value.val() (clear only — browsers block setting paths)

How to Use :file

Prefix with input (or a form scope), then count, style, or listen for changes.

  1. Prefer $("input:file") or $("#form input:file") over bare $(":file").
  2. For modern speed, use $("input[type=file]") — same matches, native CSS.
  3. Count with .length (official demo pattern).
  4. Listen with .on("change", …) and read this.files.
  5. Prevent accidental submits in demos with event.preventDefault() on the form.
PieceRequired?Role
:fileYesMatches input of type file
Tag / form scopeRecommendedAvoids 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"]

Cheat Sheet

Keep these facts handy while selecting file inputs.

TopicDetail
Syntax$("input:file")
SincejQuery 1.0
Equivalent[type="file"]
CSS?No — jQuery extension
Preferred CSS$("input[type=file]")
Bare formAvoid $(":file") → universal scan
Pick file.on("change", …) + this.files
SecurityBrowsers block setting a file path via script

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.

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();
});
Try It Yourself

How It Works

  1. Select. $("input:file") finds every file input.
  2. Count. .length is the official demo’s key number.
  3. Report. Writes the same style of message as the docs.
  4. Submit. preventDefault keeps the demo from navigating.

Example 2 — :file vs [type=file]

Same matches; the attribute selector is the faster modern choice.

jQuery
$("#out").text(
  ":file → " + $("input:file").length + "\n" +
  "[type=file] → " + $("input[type=file]").length
);
Try It Yourself

How It Works

  1. Equal. Official docs: :file ≡ [type="file"].
  2. Count. Both selectors return the same length here.
  3. Speed. Attribute form can use querySelectorAll.
  4. Prefer. Use input[type=file] in new code.

Example 3 — Scoped Form Query

Limit the search so other forms on the page stay untouched.

jQuery
$("#out").text(
  "Inside #upload: " + $("#upload input:file").length + "\n" +
  "Whole page: " + $("input:file").length
);
Try It Yourself

How It Works

  1. Scope. #upload input:file only searches that form.
  2. Global. $("input:file") includes the other form too.
  3. Habit. Always scope on multi-form pages.
  4. 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…");
Try It Yourself

How It Works

  1. Select. Bind on all input:file controls.
  2. change. Fires when the user picks a file.
  3. files. Browser FileList — use files[0].name.
  4. Safety. Check that a file exists before reading.

Example 5 — List File Input Names

Map each file control’s name attribute for a quick inventory.

jQuery
var names = $("input:file").map(function () {
  return this.name || "(unnamed)";
}).get();
$("#out").text(names.join("\n"));
Try It Yourself

How It Works

  1. Select. All file inputs in the page.
  2. map. Reads each control’s name.
  3. get. Turns the jQuery map into a plain array.
  4. Use. Handy for “which uploads exist?” checks.

Edge Cases & Pitfalls

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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
:file Universal

Bottom line: Safe everywhere jQuery runs. Prefer input[type=file] for native speed, and always prefix with input.

Performance Notes

ApproachCostBest for
$("input[type=file]")BestModern browsers — native CSS
$("input:file")FineReadable jQuery-style code
$(":file")SlowerAvoid — universal scan
$("#form input:file")CheapMulti-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.

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=…].

Next: :password Selector

Learn how to select password input fields with jQuery.

:password tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful