-1

I want to make a parent element. I have this elements, that contain tags and text. As comment I wrote what`s the type of error I become:

    var type=document.getElementsByClassName("Aut");
        console.log(type); //All elements are shown 
        var parNode=type.parentNode;
        for(var el=parNode.firstChild; el; el=el.nextSibling) {...} 
//var el=parNode.firstChild,parNode is not defined

It would be very kind, if someone explain me why this is so.

1 Answers1

1

getElementsByClassName doesn't return a single node, it returns a list of nodes, you need to do:

var type = document.getElementsByClassName("Aut");
if(type.length > 0) {
    type = type[0];
    console.log(type); //All elements are shown 
    var parNode = type.parentNode;
    var sibling = parNode;
    while( (sibling = sibling.nextSibling) != null ) { .. }
}

Also I modified your for loop, you can enumerate the parent's siblings in this manner:

var sibling = parNode;
while( (sibling = sibling.nextSibling) != null ) { .. }

This says, "so long as I keep finding a sibling (starting from parNode) keep going"

David Zorychta
  • 13,039
  • 6
  • 45
  • 81