May 14, 2012

How to count whitespaces as a childnodes?

Question by user1394479

Whitespace count as empty text element in a DOM.
How can I count these whitespaces?

for example

<body>

    <div>

    </div>

    <div>

    </div>

</body>

how many whitespaces(childNodes) inside the body element?

how many whitespaces(childNodes) inside each div element?

Answer by Starx

If you are trying to see how many elements are empty? Then I am giving you a jQuery solution instead.

bodyChild = $("body :empty").length;
divHcild = $("#divid :empty").length;

Using them, you can know what many elements are completely empty inside an element.


Update:
To get all the child element, you can use something like this

Body:

var children = document.getElementByTagName('body').childNodes;

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!