I am a bit stuck on how to reorder nodes. I am trying to add two simple "move item up" and "move item down" functions. While insertBefore() does what I want to move a sibling before the preceding one, what is the easiest way to move one node down in the DOM? Much appreciated!
2 Answers
Code Example:
try {
$li->parentNode->insertBefore( $ul, $li->nextSibling);
} catch(\Exception $e){
$li->parentNode->appendChild( $ul );
}
3 Comments
marcini
Actually, you can do only:
$li->parentNode->insertBefore( $ul, $li->nextSibling); The try..catch block is unnecessary, because if $li->nextSibling is null, insertBefore() will act just like appendChild().IanB
What if nextSibling is null (i.e. $li is the last in the series)?
nggit
If
nextSibling is null, it means DOMNode::insertBefore with second parameter omitted. Without second parameter (refnode), newnode is appended to the children. php.net/manual/en/domnode.insertbefore.php (Tested and works as the #1 comment & manual said)