jQuery :file Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

: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").

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("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
CallWhat it does
:fileMatches input of type file
Tag / form scopeAvoids a universal * scan
Equivalent[type="file"]
Return valuejQuery collection of matching file inputs

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Prefer $("input:file") or $("#form input:file") over bare $(":file").
Step 2For modern speed, use $("input[type=file]") — same matches, native CSS.
Step 3Count with .length (official demo pattern).

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

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.

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.

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.

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 Selector tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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