HTML DOM Document images-attribut

Definition och användning

images Retunerar samlningen av alla <img>-element i dokumentet.

images Property returns HTMLCollection.

imageThe s property is read-only.

Tip:The images property does not return <input> elements with type="image".

HTMLCollection

HTMLCollection It is a similar array collection (list) of HTML elements.

Elements in the collection can be accessed by index (starting from 0).

length Property returns the number of elements in the collection.

See also:

Image

HTML <img> etikett

HTML Image Tutorial

Instance

Example 1

The number of <img> elements in the document:

document.images.length;

Try it yourself

Example 2

Iterate over all <img> elements and output the URL (src) of each element:

const myImages = document.images;
let text = "";
for (let i = 0; i < myImages.length; i++) {
  text += myImages[i].src + "<br>";
}

Try it yourself

Example 3

The URL of the first <img> element is:

document.images[0].src;

Try it yourself

Example 4

The URL of the first <img> element is:

document.images.item(0).src;

Try it yourself

Example 5

The URL of the <img> element with id="myImg" is:

document.images.namedItem("myImg").src;

Try it yourself

Example 6

Add a black border to the first <img> element:

document.images[0].style.border = "10px dotted black";

Try it yourself

Syntax

document.images

Property

Property Description
length The number of <img> elements in the collection.

Method

Method Description
[index]

Returns the element at the specified index (starting from 0).

Returns null if the index is out of range.

item(index)

Returns the element at the specified index (starting from 0).

Returns null if the index is out of range.

namedItem(id)

Returns an element with the specified id element.

If id Returns null if it does not exist.

Return value

Type Description
Object

HTMLCollection object.

All <img> elements in the document.

They are sorted in the order they appear in the source code.

Browser support

document.images It is a DOM Level 1 (1998) feature.

All web browsers support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Stöd 9-11 Stöd Stöd Stöd Stöd