I’m trying to figure out how to know if a node is the last child of another, or not.
I know how to retrieve the parent’s lastChild name ($node->parentNode->lastChild->nodeName), but as sometimes there’s many same node names within the same parent, the name is definitly not the way to find the last child.
I’m looking for some kind of iterator which can retrieve the number of childs, and depending of the position of the current node, tell if the current node is the last child.
Thanks!
In pure DOM terms, if the child is the last child of a parent, its
nextSiblingproperty will benull.Javascript example (not that you’re looking for Javascript specifically):
It’s important to note that
nextSiblingtraverses nodes. Sometimes you want to traverse elements instead (as opposed to text nodes and such). For instance, in this markup:…the
spanis not the last node in its container (because it’s followed by a text node), but it is the last element in its container. If you want to look for that, you need a loop checking thenodeType, e.g.: