Elementals.js

Latest Version: 3.7 Final 20 December 2018

Method _.Node.countChildren

Summary:

Counts the number of direct child elements inside the target element, using a nodeType or tagName mask.

Calling Convention:
_.Node.countChildren(Element[, nodeMask || tagName]]])
Parameters:
Element
The target Element to count the nodes inside of.
nodeMask optional

A bitwise mask used to filter the results. See the Node Masking page for more information.

If omitted, a mask of 1 (matching for nodeType 1) is used.

tagName optional
A tagName to match. When used instead of a nodeMask only elements of node type 1 with a matching tagName will be counted.
Returns:
A integer count of the number of matching DOM elements that are direct children of Element

Example #1

JavaScript

<div id="test">
	<div>
		<h2>Section 1</h2>
		<p>This is a <strong>test</strong></p>
		<p>This is another paragraph</p>
	</div>
	<p>Yet another paragraph</p>
</div>

JavaScript

Console.log(_.Node.countChildren(_d.getElementById('test'))); // 2
Console.log(_.Node.countChildren(_d.getElementById('test'), 'p')); // 1

A result of 2 is reported by the first log entry because by default only nodeType 1 is matched and for direct children there's the div and the p. That inner DIV's contents don't count, to get that number use _.Node.countChildren. The single result for paragraphs is becuase there's only one paragraph as a direct child. Again the contents of that DIV don't count!