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!
PHP DOMNode insertAfter?
Asked Answered
Code Example:
try {
$li->parentNode->insertBefore( $ul, $li->nextSibling);
} catch(\Exception $e){
$li->parentNode->appendChild( $ul );
}
What if nextSibling is null (i.e. $li is the last in the series)? –
Pastiness
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) –
Mobility Okay, stupid me. The easy solution is to just go down in the DOM to the nextSibling of the nextSibling and do the same insertBefore... so this is solved.
© 2022 - 2024 — McMap. All rights reserved.
$li->parentNode->insertBefore( $ul, $li->nextSibling);
The try..catch block is unnecessary, because if $li->nextSibling is null, insertBefore() will act just like appendChild(). – Urano