35

i have problem with childNodes as below :

 <ol>
    <li>Coffee</li>
    <li>Tea</li>
    <li>Coca Cola</li>
 </ol>
 //childNodes.length = 7

but

<ol><li> Coffee </li><li> Tea </li><li> Coca Cola </li></ol>
//childNodes.length = 3

It seems each \n or textnode is considered a child ,how can i remove these from childNodes?

5
  • 6
    You can use nodeType. Text Nodes will have nodeType = 3. Demo. Commented Jul 26, 2014 at 12:49
  • this may be useful ,i tried to detect \n but can't Commented Jul 26, 2014 at 12:54
  • @Harry - Your comment actually qualifies as an answer. Why not post it as one? Commented Jul 26, 2014 at 12:54
  • 2
    You may use children Commented Jul 26, 2014 at 12:56
  • 1
    @Harry - Ok. But your answer is an accurate solution for the question title "javascript check if child node is element or text node" Commented Jul 26, 2014 at 12:58

4 Answers 4

61

You can check if a given child node is a text node or not using the nodeType. Text nodes will have the nodeType as 3. We can either use the number or the constant Node.TEXT_NODE for checking.

window.onload = function() {
  var el = document.getElementsByTagName('ol')[0].childNodes; // using [0] as there is only one ol in the demo
  console.log('Print with text nodes');
  for (let i = 0; i < el.length; i++) { // will output all nodes with "undefined" for text nodes
    console.log(el[i].innerHTML);
  }
  console.log('Print without text nodes');
  for (var i = 0; i < el.length; i++) { // will output only non text nodes.
    if (el[i].nodeType !== Node.TEXT_NODE) // or if (el[i].nodeType !== 3)
      console.log(el[i].innerHTML);
  }
}
<ol>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Coca Cola</li>
</ol>

Sign up to request clarification or add additional context in comments.

2 Comments

Hey good answer, you can also use the constant Node.TEXT_NODE instead of the meaningless 3.
@A1rPun: Nice suggestion. Will update into the answer. Thanks :)
5

Text nodes can also be checked for using nodeName

node.nodeName === '#text'

Comments

0

While the other answers discuss how it's technically possible to detect if a child node is a text node or not, in your example case given it would appear you want to simply get the element node types, in which case you can simply use the children property.

In the question itself, it doesn't say why childNodes is necessary.

Element.children includes only element nodes. To get all child nodes, including non-element nodes like text and comment nodes, use Node.childNodes.

Comments

-5

try to use jquery children method
$("#test").children().size()

http://jsfiddle.net/72Ya3/2/

2 Comments

This trivial requirement does not need jQuery. The other answer is way more correct as well as informative.
This can be done with plain JavaScript: element.nodeType === 3 // true when element is a text node. See @Harry's answer, which should likely be the accepted one, and the MDN article on nodeType.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.