PHP DOMNode insertAfter?
Asked Answered
H

2

17

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!

Hanley answered 24/4, 2014 at 15:48 Comment(0)
D
23

Code Example:

 try {
        $li->parentNode->insertBefore( $ul, $li->nextSibling);
 } catch(\Exception $e){
        $li->parentNode->appendChild( $ul );
 }
Down answered 21/5, 2015 at 13:47 Comment(3)
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().Urano
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
H
0

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.

Hanley answered 24/4, 2014 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.