Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing element places in jQuery [duplicate]

Tags:

html

jquery

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");
like image 865
newbie Avatar asked Dec 10 '25 09:12

newbie


1 Answers

This should be enough:

first.appendTo(first.parent());
last.prependTo(last.parent());

Here a jsFiddle

like image 108
DanielB Avatar answered Dec 11 '25 22:12

DanielB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!