jQuery :file Selector

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Form controls

What You’ll Learn

The :file selector matches every input element with type="file" — the upload control in HTML forms. It is a jQuery form pseudo-class since 1.0, equivalent to [type="file"].

01

Syntax

input:file

02

type

= file

03

Prefix

Use input

04

Official

Style + count

05

Upload

change event

06

CSS alt

[type=file]

Introduction

File inputs power uploads — profile photos, document attachments, CSV imports. When a form mixes text fields, checkboxes, and file pickers, you need a selector that targets only the upload controls.

jQuery’s :file pseudo-class selects all elements of type file — in practice, every <input type="file">. It has existed since jQuery 1.0 and is equivalent to $("[type=file]"). Official docs recommend input:file rather than bare :file, which implies *:file.

Understanding the :file Selector

Think of :file as a shortcut for “every file upload input in this scope.” It filters by the HTML type attribute — nothing else.

  • <input type="file"> → matches.
  • <input type="file" accept="image/*"> → matches (accept does not affect selector).
  • <input type="text"> → no match.
  • <input type="checkbox"> → no match (use :checkbox).
  • <button> → no match.
💡
Beginner Tip

For performance in modern browsers, official jQuery docs suggest input[type="file"] instead of :file so the initial query can use native querySelectorAll.

📝 Syntax

Official jQuery API form (since 1.0):

jQuery
jQuery( "input:file" )

// scoped (recommended):
$( "form input:file" )
$( "#uploadForm input:file" )

// CSS equivalent (faster in modern browsers):
$( 'input[type="file"]' )

Parameters

  • No arguments — the pseudo-class matches by input type attribute.

Return value

  • A jQuery object containing every file input in the search context.
  • Empty collection when no file inputs exist.

Official jQuery API example

jQuery
var input = $( "input:file" ).css({
  background: "yellow",
  border: "3px red solid"
});

$( "div" )
  .text( "For this type jQuery found " + input.length + "." )
  .css( "color", "red" );

⚡ Quick Reference

Elementinput:fileinput[type="file"]
<input type="file">MatchesMatches
<input type="file" multiple>MatchesMatches
<input type="text">No matchNo match
<input type="button">No matchNo match
Native querySelectorAllNot supportedSupported

📋 :file vs [type="file"] and other input types

jQuery pseudo-class vs attribute selector vs sibling form types.

:file
input:file

jQuery extension

[type=file]
input[type="file"]

Native CSS — faster

:checkbox
input:checkbox

Different type

:button
:button

Buttons, not file

Examples Gallery

Example 1 follows the official jQuery input:file demo. Examples 2–5 compare with attribute selectors, count scoped uploads, handle the change event, and use the native performance pattern.

📚 Official jQuery Demo

Style file inputs and report how many were found.

Example 1 — Official Demo: input:file

Official jQuery demo — yellow background, red border, and match count.

jQuery
var input = $( "input:file" ).css({
  background: "yellow",
  border: "3px red solid"
});

$( "div" ).text( "For this type jQuery found " + input.length + "." );
Try It Yourself

How It Works

jQuery collects every input[type=file], applies inline styles, then .length reports the count — skipping text, checkbox, and other types.

Example 2 — :file vs input[type="file"]

Verify both selectors return the same count — official docs call them equivalent.

jQuery
console.log( ":file →", $( "input:file" ).length );
console.log( "[type=file] →", $( 'input[type="file"]' ).length );
Try It Yourself

How It Works

Both expressions select the same DOM nodes — choose attribute syntax when you need native selector performance.

📈 Practical Patterns

Scoped counting, upload UI, and performance.

Example 3 — Count in Form: $("#uploadForm input:file").length

Count file inputs inside one form — ignore file inputs elsewhere on the page.

jQuery
var n = $( "#uploadForm input:file" ).length;
$( "#fileCount" ).text( n + " file input(s) in the form" );
Try It Yourself

How It Works

Scoping with a form ID limits the matched set before counting — essential on pages with multiple upload widgets.

Example 4 — Upload Feedback: $("input:file").on("change")

Show the selected filename when the user picks a file.

jQuery
$( "input:file" ).on( "change", function() {
  var name = this.files.length ? this.files[0].name : "none";
  $( "#status" ).text( "Selected: " + name );
});
Try It Yourself

How It Works

Select file inputs with :file, then use the native files property on the change event — jQuery handles binding; the File API reads the name.

Example 5 — Performance: input[type="file"]

Official docs — use the CSS attribute selector for native querySelectorAll speed.

jQuery
$( 'input[type="file"]' ).addClass( "picked" );

// Equivalent to input:file — better for browser optimization
Try It Yourself

How It Works

Because :file is a jQuery extension, embedding it blocks native optimization. Attribute selectors are standard CSS.

🚀 Common Use Cases

  • Upload forms — style or validate all input:file fields.
  • Multi-file widgets — count file inputs before submit.
  • Filename preview — bind change on selected file inputs.
  • Custom upload UI — hide native input, trigger click via jQuery.
  • Form reset — clear file inputs with .val("") on the matched set.
  • Accept filters — combine with [accept] for image-only pickers.

🧠 How jQuery Evaluates :file

1

Find candidates

Start from scoped inputs — input:file, not bare :file.

Scope
2

Check type attribute

Keep only input elements whose type is file.

Filter
3

Return collection

All matching file inputs in document order within scope.

Result
4

Chain methods

Apply .css(), .on("change"), or .prop() on upload controls.

📝 Notes

  • Available since jQuery 1.0 — selects input[type=file] only.
  • Equivalent to [type="file"] — official jQuery documentation.
  • Prefer input:file over bare $(":file") (implies *:file).
  • Not valid in native querySelectorAll(":file") — use attribute selector instead.
  • accept, multiple, and capture attributes do not change whether :file matches.
  • Security: browsers restrict reading file paths — use the files API after selection.

Browser Support

The :file pseudo-class is a jQuery extension — not standard CSS — and works in jQuery 1.0+. File inputs themselves are supported in all modern browsers. For native selector speed, use input[type="file"].

jQuery 1.0+

jQuery :file Selector

Works wherever jQuery and HTML file inputs are supported. Native: input[type=file].

100% jQuery + HTML
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: Use input:file or input[type=file] scoped to your form — not bare :file on the whole document.

Conclusion

The :file selector matches every file upload input — <input type="file"> — in your search context. The official demo styles them and reports how many were found.

Prefix with input, scope to your form, and prefer input[type="file"] when you need native selector performance. Pair with change handlers for upload UX.

💡 Best Practices

✅ Do

  • Use $("#form input:file") for scoped queries
  • Prefer input[type="file"] for performance
  • Bind change for upload feedback
  • Use accept to guide file types
  • Clear with .val("") to reset selection

❌ Don’t

  • Use bare $(":file") on large documents
  • Expect :file to match non-input elements
  • Confuse :file with :button or :checkbox
  • Read full file paths from the value attribute (blocked by browsers)
  • Forget to scope when multiple forms exist on one page

Key Takeaways

Knowledge Unlocked

Five things to remember about :file

All file upload inputs in scope.

5
Core concepts
in 02

Prefix

input:file

Scope
[] 03

Equiv

[type=file]

CSS
Δ 04

change

Upload UX

Tip
demo 05

Official

Count demo

Demo

❓ Frequently Asked Questions

:file selects every input element whose type attribute is file — the control users click to upload files from their device. $("input:file") finds all file inputs in the search context. Available since jQuery 1.0.
Yes. Official jQuery docs state that :file is equivalent to [type="file"]. For native querySelectorAll performance in modern browsers, prefer input[type="file"] over the :file pseudo-class.
Prefer $("input:file"). Bare $(":file") implies the universal selector — equivalent to $("*:file") — which scans every element type. Official docs recommend prefixing with input or scoping to a form.
No. It is a jQuery extension since version 1.0. Native querySelectorAll cannot run :file directly. Use input[type="file"] for standard CSS attribute matching.
The official jQuery form demo styles $("input:file") with a yellow background and red border, then reports how many were found — typically one file input among text, button, checkbox, and other controls in the sample form.
Yes. After selecting with $("input:file"), bind the change event — e.g. $("input:file").on("change", function() { var name = this.files[0].name; }) — to react when the user picks a file.
Did you know?

Official jQuery docs note that :file cannot use native querySelectorAll optimization — but input[type="file"] can. In upload-heavy pages, switching to the attribute selector for the initial query often improves performance while keeping the same matched elements.

Continue to :password Selector

After file upload inputs, learn how to select masked password fields in forms.

:password selector 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.

6 people found this page helpful