2

Is there any built in function in JavaScript to get only element nodes, or do I have to add another loop which counts the actual number of those if I need it before, like:

l = 0;
for(i = 0; i < x.childNodes.length; i++){
    if(x.childNodes[i].nodeType = 1) l++;       
}

for(i = 0; i < x.childNodes.length; i++){
    if(x.childNodes[i].nodeType != 1) continue;
    new Something(l);   
}
1
  • 2
    Try .children instead of .childNodes Commented Sep 5, 2015 at 16:56

1 Answer 1

1

Node.children is a read-only property that returns a live HTMLCollection of the child elements of Node.

Syntax

var elList = elementNodeReference.children; 

elList is a HTMLCollection, which is an ordered collection of DOM elements that are children of elementNodeReference. If there are no element children, then elList contains no elements and has a length of 0.

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

1 Comment

This is what i wanted but should I change the question title because I accepted the question but it not match the title exactly?

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.