Document images Collection - Javascript DOM

Javascript examples for DOM:Document images

Description

The images collection returns all <img> elements in the document are sorted as they appear in the source code.

The images collection does not return a collection of <input> elements with type="image".

Syntax

Properties

Property Description
length Returns the number of <img> elements.

Methods

MethodDescription
[index] <img> element with the specified index (starts at 0).
item(index) <img> element with the specified index (starts at 0).
namedItem(id) <img> element with the specified id.

Return Value:

An HTMLCollection Object, representing all <img> elements sorted as they appear in the HTML code

The following code shows how to Find out how many <img> elements there are in the document:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<img src="http://java2s.com/resources/a.png" alt="flower" width="150" height="113">
<img src="http://java2s.com/resources/b.png" alt="flower" width="152" height="128">
<img id="myImg" src="http://java2s.com/resources/c.png" alt="Smiley face" width="42" height="42">

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*from   w  w  w.ja  v  a2s  . c om*/
    var x = document.images[0];
    x.style.border = "10px dotted black";
}
</script>

</body>
</html>

Related Tutorials