2

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");
Community
  • 1
  • 1
newbie
  • 24,286
  • 80
  • 201
  • 301

3 Answers3

6

This should be enough:

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

Here a jsFiddle

DanielB
  • 19,910
  • 2
  • 44
  • 50
3
 $('#item-1').after($('#item-3'));
 $('#item-2').after($('#item-1'));
BonyT
  • 10,750
  • 5
  • 31
  • 52
2

Look at this question Is there a native jQuery function to switch elements?

Community
  • 1
  • 1
mark-cs
  • 4,677
  • 24
  • 32