I am creating a website of floating width. The users use screens from full HD resolution to some 600px on smart phones it seems a pretty good idea. This brings up a very interesting problem.
When user uses a smaller resolution than is an optimum the page gets a lot more height. This means it might be useful to change order of some elements (for example some image, search box or navigation) to make the page more readable without much need of scrooling.
So I need to be able to access DOM and change order of some page elements (swap them).
Lets say I have an list and need to swap item 1 and 2.
<ul>
<li>1</li>
<li>2</li>
</ul>
I found a solution based on appending already items elements to <ul> by using function appendChild. However there is a problem with text nodes and it gets really complicated to do it for more difficult element structure, since the need of recreating it whole again.
Do you have any suggestion to improve it?
For this simple case (swapping the only two elements), you can just use
appendChild():The same node cannot exist in multiple positions; so, it’s removed from its current position and placed at the end of the collection.
If you want to do more complicated sorting, you probably ought to create an array from the childNodes and get all crazy: