JavaScript to count number of images
I have certain queries in my JavaScript project. There will be multiple images on my form. These will be selected by the user on the form prior to this form. When the user comes on this form of images it should display how many images are present in the current form. So the user is not needed to count his/her selection. I am unable to code this.
Re: JavaScript to count number of images
Images Object Array is supported in JavaScript 1.1 version of JavaScript. The Image Object Array is a collection of all the images in a document. To determine the total number of images displayed in the document we need to use the length property. Thus if you need to count the number of images using JavaScript use the length property of Image Object Array.
document.images.length to count image
I too had to keep a track of the image count in my JScript project where the images resembled the choices made by the visitor and I had to tally them all at the end. I tried many things and finally this piece of code helped me to get the image count:
Code:
<script language="JavaScript1.1">
document.write(document.images.length);
</script>
Include the above code in your project to enable image count in a document. You may print or save the image count to be used later.
counting images in JavaScript
I had an assignment question where in it was required to a count of images on a web page which was to be scripted using JavaScript. We were given two images on a document and had to count them to display the image count as 2. This is the program that I did:
Code:
<html>
<body>
<img alt="Virus" border="0" src="Pic1.gif" width="70" height="70" />
<br />
<img alt="Worm" border="0" src="Pic2.gif" width="50" height="50" />
<br /><br />
<script type="text/javascript">
document.write("The image count of this document is: " + document.images.length);
</script>
</body>
</html>