Possible Duplicate:
Is there a native jQuery function to switch elements?
I have list of items in my HTML, I want to change places of first and last element, but how can I do that with jQuery? I can get first and last elements, but how can I change their places (order) in list?
Example HTML:
<ul>
<li id="item-1">1</li>
<li id="item-2">2</li>
<li id="item-3">3</li>
</ul>
jQuery:
var first = $("ul li:first");
var last = $("ul li:last");
This should be enough:
first.appendTo(first.parent());
last.prependTo(last.parent());
Here a jsFiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With