HTML DOM length Property

HTMLWeb DevelopmentFront End Technology

The HTML DOM length property returns a number corresponding to the nodes in node list object.

Syntax

Following is the syntax −

Returning number of nodes in nodeList Object.

nodeList.length

Example

Let us see an example for HTML DOM length property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM length</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
   ul{
      width: 30%;
      margin: 0 auto;
   }
</style>
</head>
<body>
<form>
<fieldset>
<legend>HTML-DOM-length</legend>
<h3>Students</h3>
<ul>
<li>Adam</li>
<li>Alex</li>
<li>Bina</li>
<li>Eden</li>
<li>Rajesh</li>
<li>Zampa</li>
</ul>
<input type="button" onclick="getStrength()" value="Get Total Students">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var studentList = document.getElementsByTagName("li");
   function getStrength() {
      divDisplay.textContent = 'Total Students: '+studentList.length;
   }
</script>
</body>
</html>

Output

This will produce the following output −

Before clicking ‘Get Total Students’ button −

After clicking ‘Get Total Students’ button −

raja
Published on 30-Jul-2019 15:22:06
Advertisements