Find out how many <img> elements there are in the document:
The result of x will be:
More "Try it Yourself" examples below.
The images collection returns a collection of all <img> elements in the document.
Note: The elements in the collection are sorted as they appear in the source code.
Note: The images collection does not return a collection of <input> elements with type="image".
Tip: Also look at the Image Object.
Collection | |||||
---|---|---|---|---|---|
images | Yes | Yes | Yes | Yes | Yes |
Property | Description |
---|---|
length | Returns the number of <img> elements in the collection. Note: This property is read-only |
Method | Description |
---|---|
[index] | Returns the <img> element from the collection with the specified index
(starts at 0). Note: Returns null if the index number is out of range |
item(index) | Returns the <img> element from the collection with the specified index
(starts at 0). Note: Returns null if the index number is out of range |
namedItem(id) | Returns the <img> element from the collection with the specified id. Note: Returns null if the id does not exist |
DOM Version: | Core Level 1 Document Object |
---|---|
Return Value: | An HTMLCollection Object, representing all <img> elements in the document. The elements in the collection are sorted as they appear in the source code |
[index]
Get the URL of the first <img> element (index 0) in the document:
The result of x will be:
item(index)
Get the URL of the first <img> element (index 0) in the document:
The result of x will be:
namedItem(id)
Get the URL of the <img> element with id="myImg" in the document:
The result of x will be:
Add a black dotted border to the first <img> element in the document:
Loop through all <img> elements in the document, and output the URL (src) of each image:
The result of txt will be:
JavaScript reference: HTML DOM Image Object
HTML tutorial: HTML Images
HTML reference: HTML <img> tag