How to return the number of images in a document with JavaScript?


To return the number of images in a document, use the images property in JavaScript.

Example

You can try to run the following code to get the count of images −

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <img src="https://www.tutorialspoint.com/html5/images/html5-mini-logo.jpg">
      <img src="https://www.tutorialspoint.com/hive/images/hive-mini-logo.jpg">
      <img src="https://www.tutorialspoint.com/sas/images/sas-mini-logo.jpg">
      <img src="https://www.tutorialspoint.com/maven/images/maven-mini-logo.jpg">
      <script>
         var val = document.images.length;
         document.write("<br>Number of images in the document: "+val);
      </script>
   </body>
</html>

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

810 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements