const unorderedList = $('ul');
const listItems = unorderedList.children('li');
unorderedList.prepend(listItems.slice(Math.round(listItems.length / 2)))
Example: http://jsfiddle.net/8SNGn/
This script first saves the reference to the <ul>
element and its child <li>
elements using the children()
[docs] method.
Then it utilizes the prepend()
[docs] method to insert the last li
elements at the beginning of the ul
.
To select the last half of elements, it divides the total number of li
elements in two (rounding up with Math.round()
for odd numbers) using the slice()
[docs] method.
EDIT:
I noticed that your question contains the term "clone".
Although not explicitly stated, if you wish to clone the last half instead, you can achieve this by utilizing the clone()
[docs] method.
const unorderedList = $('ul');
const listItems = unorderedList.find('li');
unorderedList.prepend(listItems.slice(Math.round(listItems.length / 2)).clone())
Example: http://jsfiddle.net/8SNGn/1/
If you prefer rounding down instead, you can substitute Math.floor()
for Math.round
.