Technology Software

How to Find out How Many Images Are in a DIV With JavaScript

    • 1). Create an HMTL file in an text editor or in Notepad. Create a <div> tag with an "id" attribute and place <img> tags inside the <div> tag. For example, type

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml">

      <head>

      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

      <title>Count Images</title>

      </head>

      <body>

      <div><img src="http://example.com/image1.jpg" alt="image1" /><img src="http://example.com/image2.jpg" alt="image2" /><img src="http://example.com/image3.jpg" alt="image3" /></div>

      </body>

      </html>

    • 2). Create a JavaScript function and place it between the <head> tags. Get the contents of the <div> tag and store it in a variable. For example, type

      <script type="text/javascript">

      function countImages() {

      var divContents = document.getElementById("images").innerHTML;

    • 3). Use a regular expression to get array elements that match the opening of the <img> tag. For example, type

      var matches = divContents.match(/<img/);

    • 4). Get the number of images by referencing the number of elements in the array. For example, type

      var numMatches = matches.length;

      alert("Number of matches is " + numMatches);

      }

      </script>

    • 5). Call the JavaScript function right before the closing <body> tag to give the HTML page time to load. For example, type:

      <script type="text/javascript">

      countImages();

      </script>

SHARE
RELATED POSTS on "Technology"
How Do I Print Business Cards With Microsoft Publisher?
How Do I Print Business Cards With Microsoft Publisher?
How to Reduce the Size of Photos Using Adobe Photoshop Elements 3.0
How to Reduce the Size of Photos Using Adobe Photoshop Elements 3.0
Features and Benefits of Project Portfolio Management
Features and Benefits of Project Portfolio Management
Fix Registry - Fix Registry Errors Easily
Fix Registry - Fix Registry Errors Easily
Business Intelligence Solutions and Services
Business Intelligence Solutions and Services
AutoCAD Architecture
AutoCAD Architecture
Best Social Networking Apps
Best Social Networking Apps
IKE
IKE
Help Desk Software by help-desk-software
Help Desk Software by help-desk-software
What's Better - Building a BPM Solution Or Buying One?
What's Better - Building a BPM Solution Or Buying One?
How to Make Music Fade out in Moviemaker
How to Make Music Fade out in Moviemaker
How to Shrink a DVD With Nero Instructions
How to Shrink a DVD With Nero Instructions
Live Chat Software: Four Tips To Help You Get the Most Out of Your Software
Live Chat Software: Four Tips To Help You Get the Most Out of Your Software
How to Stop Rootkits
How to Stop Rootkits
3 First-Rate Foundations for Making Movies
3 First-Rate Foundations for Making Movies
How to Build Game Downloads
How to Build Game Downloads
The Linux Modem How-To
The Linux Modem How-To
Text-Terminals on Linux - 11.6 Terminal Server Connection
Text-Terminals on Linux - 11.6 Terminal Server Connection
The Linux Loadable Kernel Module How-To
The Linux Loadable Kernel Module How-To
How to Create Photo Albums With CSS
How to Create Photo Albums With CSS

Leave Your Reply

*