HTML DOM childElementCount property The childElementCount property returns the number of child elements of the specified element (excluding text nodes and comment nodes). grammar: element.childElementCount Example: var div = document.getElementById('myDiv'); var count = div.childElementCou

WBOY
Release: 2023-09-23 12:45:02
forward
1077 people have browsed it

HTML DOM childElementCount property is a read-only property that returns the number of child elements of a given element. The return type of childElementCount is unsigned long. It will only return the child elements of the queried node, not all child nodes of the HTML document.

Syntax

The following is the syntax of childElementCount attribute-

node.childElementCount
Copy after login

Example

Let us see an example of HTML DOM childElementCount attribute-

<!DOCTYPE html>
<html>
<head>
<style>
   div {
      border: 2px solid blue;
      margin: 7px;
      padding-left:20px;
   }
</style>
</head>
<body>
<p>Click the button below to find out the no of children of the div element</p>
<button onclick="childCount()">COUNT</button>
<div id="myDIV">
<h3>HEADING</h3>
<p>First p element</p>
<p>Second p element</p>
</div>
<p id="Sample"></p>
<script>
   function childCount() {
      var x = document.getElementById("myDIV").childElementCount;
      document.getElementById("Sample").innerHTML = "The div element has "+x+" children";
   }
</script>
</body>
</html>
Copy after login

Output

This will produce the following output-

HTML DOM childElementCount 属性

childElementCount 属性返回指定元素的子元素数量(不包括文本节点和注释节点)。

语法:
element.childElementCount

示例:
var div = document.getElementById(

When the Count button is clicked-

HTML DOM childElementCount 属性

childElementCount 属性返回指定元素的子元素数量(不包括文本节点和注释节点)。

语法:
element.childElementCount

示例:
var div = document.getElementById(

In the above example -

We created an element with the id "myDIV" and three elements within it. Two

elements and a

header. We also added colored borders, margins and padding to the div to distinguish it from other elements -

div {
   border: 2px solid blue;
   margin: 7px;
   padding-left:20px;
}
<div id="myDIV">
<h3>HEADING</h3>
<p>First p element</p>
<p>Second p element</p>
</div>
Copy after login

Then we created a button COUNT which will execute the childCount() method when clicked .

<button onclick="childCount()">COUNT</button>
Copy after login

childCount() method gets the element with id "myDIV" ( in our case ) element and assigns its childElementCount attribute value to variable x. Since there are two

elements and one

element in , childElementCount returns 3.

The returned value is then displayed in the paragraph with the id "Sample" using the innerHTML () method in the paragraph -

function childCount() {
   var x = document.getElementById("myDIV").childElementCount;
   document.getElementById("Sample").innerHTML = "The div element has "+x+" children";
}
Copy after login

The above is the detailed content of HTML DOM childElementCount property The childElementCount property returns the number of child elements of the specified element (excluding text nodes and comment nodes). grammar: element.childElementCount Example: var div = document.getElementById('myDiv'); var count = div.childElementCou. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template