JavaScript Document images Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.images is a read-only instance property that returns a live HTMLCollection of every <img> in the document. Learn MDN’s banner search loop, index vs item() access, live updates, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

HTMLCollection

03

Items

HTMLImageElement

04

Live

Updates

05

Access

[0] / item()

06

Status

Baseline widely

Introduction

Need every image on a page without writing a selector? document.images gives you a ready-made collection of all <img> elements.

MDN: the read-only property returns a collection of the images in the current HTML document. Each entry is an HTMLImageElement representing a single image element.

💡
Live collection

Like document.forms and document.embeds, the list updates when images are added or removed from the DOM.

Related Document tutorials: forms, embeds, anchors, Document constructor.

Understanding Document.images

A read-only instance property on Document. Its value is a live HTMLCollection of image elements.

  • Value — live HTMLCollection of all <img> elements (MDN).
  • Each itemHTMLImageElement with src, alt, width, etc.
  • Accessdocument.images[0] or document.images.item(0) (MDN: equivalent).
  • Lengthdocument.images.length counts images on the page.
  • Iteratefor...of document.images works in modern browsers.

📝 Syntax

JavaScript
document.images

Value

An HTMLCollection — a live list of every HTMLImageElement in the document (MDN).

MDN example

JavaScript
for (const image of document.images) {
  if (image.src === "banner.gif") {
    console.log("Found the banner");
  }
}

Note: image.src is usually an absolute URL in the browser, so production code often uses image.src.endsWith("banner.gif") or compares the filename.

⚡ Quick Reference

GoalCode / note
Count imagesdocument.images.length
First imagedocument.images[0]
Same as item()document.images.item(0)
Loop allfor (const img of document.images) { ... }
Read src / altdocument.images[i].src
MDN statusBaseline Widely available (since Jun 2018)

🔍 At a Glance

Four facts about document.images.

Type
HTMLCollection

Read-only

Items
HTMLImageElement

<img> only

Live?
yes

Auto-updates

Status
baseline

Widely available

📋 document.images vs querySelectorAll

document.imagesquerySelectorAll("img")
Return typeHTMLCollectionNodeList
Live?YesNo (static snapshot)
ScopeAll img in documentAny CSS selector
When to useQuick all-images accessFiltered or scoped queries

Examples Gallery

Examples follow MDN Document: images. Each item in the collection is an HTMLImageElement.

📚 Getting Started

Count images and use MDN’s search loop.

Example 1 — Count Images with length

How many <img> elements are on the page?

JavaScript
console.log(document.images.length);
console.log("Has images?", document.images.length > 0);
Try It Yourself

How It Works

length is the fastest way to see whether a page includes any images.

Example 2 — MDN: Find banner.gif

Loop the collection and match by src (MDN pattern).

JavaScript
for (const image of document.images) {
  if (image.src.endsWith("banner.gif")) {
    console.log("Found the banner");
  }
}
Try It Yourself

How It Works

MDN compares image.src directly; browsers resolve URLs, so endsWith() is more reliable in real pages.

📈 Access, Alt Text & Live Updates

Index access, accessibility checks, and the live collection.

Example 3 — First Image: [0] vs item(0)

MDN: array notation and item() are equivalent.

JavaScript
const firstByIndex = document.images[0];
const firstByItem = document.images.item(0);

console.log(firstByIndex === firstByItem);
console.log(firstByIndex?.src);
Try It Yourself

How It Works

Use optional chaining (?.) when the page might have zero images.

Example 4 — List Every Image alt Text

Audit accessibility across all images on the page.

JavaScript
for (const img of document.images) {
  console.log(img.alt || "(missing alt)");
}
Try It Yourself

How It Works

Decorative images should use empty alt=""; informative images need descriptive alt text.

Example 5 — Live Collection Updates

Append a new <img> and watch length grow.

JavaScript
console.log("before:", document.images.length);

const img = document.createElement("img");
img.src = "https://via.placeholder.com/48";
img.alt = "Added dynamically";
document.body.appendChild(img);

console.log("after:", document.images.length);
Try It Yourself

How It Works

MDN: the collection is live — no need to re-query after DOM changes.

🚀 Common Use Cases

  • Image galleries — count or iterate all thumbnails.
  • Lazy-load audits — inspect loading attributes on every img.
  • Accessibility — find images missing alt text.
  • Preload checks — verify banner or hero images loaded.
  • CMS / admin tools — report how many images a page contains.
  • Legacy scripts — quick all-images access without selectors.

🧠 How document.images Works

1

Page loads img elements

Each <img> becomes an HTMLImageElement in the DOM.

Parse
2

You read document.images

Browser returns a live HTMLCollection (MDN).

Access
3

Loop or index into items

Use [i], item(i), or for...of on each image.

Use
4

Collection stays live

Add or remove <img> nodes and document.images reflects the change automatically.

📝 Notes

  • MDN: Baseline Widely available (since June 2018) — no Deprecated / Experimental / Non-standard banner.
  • Only <img> elements — not CSS backgrounds or inline SVG (unless wrapped in img).
  • image.src is typically an absolute URL; compare filenames carefully (MDN example).
  • document.images[i] and document.images.item(i) are equivalent (MDN).
  • Related: forms, embeds, hidden, Document constructor.

Universal Browser Support

Document.images is marked Baseline Widely available on MDN (since June 2018). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.images

Read-only live HTMLCollection of every img element in the document.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in legacy IE
Full support
Document.images Excellent

Bottom line: Use document.images for quick access to all img elements. Prefer querySelectorAll when you need filtered or scoped queries.

Conclusion

Document.images is a simple, live shortcut to every <img> on the page. Use it to count, loop, and inspect HTMLImageElement properties — or reach for querySelectorAll when you need more specific queries.

Continue with implementation, forms, hidden, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use document.images.length for a quick count
  • Loop with for...of in modern code
  • Use endsWith() or URL parsing when matching src
  • Check alt text for accessibility audits
  • Remember the collection is live after DOM changes

❌ Don’t

  • Assume image.src equals a relative path string
  • Expect CSS background images to appear in the collection
  • Cache length across long loops if DOM may change mid-loop
  • Confuse live HTMLCollection with static NodeList behavior
  • Skip alt on informative images you add dynamically

Key Takeaways

Knowledge Unlocked

Five things to remember about document.images

All img elements in one live collection.

5
Core concepts
🖼02

Items

HTMLImageElement

<img>
🔄03

Live

Auto-updates

MDN
🔢04

Access

[0] / item()

Equivalent
🔍05

Loop

for...of

MDN

❓ Frequently Asked Questions

A read-only HTMLCollection of every <img> element in the document. Each item is an HTMLImageElement (MDN).
No. MDN marks Document.images as Baseline Widely available (since June 2018). It is a standard Document collection property.
Yes. MDN describes it as a live list — when images are added or removed, document.images updates automatically.
Use document.images[0] or document.images.item(0). MDN notes both are equivalent.
document.images is a live HTMLCollection of img elements only. querySelectorAll returns a static NodeList and accepts any CSS selector.
No. Only <img> elements in the HTML document — not background-image styles or SVG images unless they use an <img> tag.
Did you know?

document.images is one of several legacy-named Document collections (along with forms, links, and anchors) that predate modern querySelector APIs—but remain widely supported and convenient for all-images access.

Next: implementation

Learn the DOMImplementation factory via document.implementation.

implementation →

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