Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto change position of <li> in Jquery

I have an item list in html.

<ul>
<li id="1">1</li>
<li id="2">2</li>
<li id="3">3</li>
<li id="4">4</li>

</ul>

I want a jquery to to auto move <li> positon like that

<ul>
<li id="2">2</li>
<li id="3">3</li>
<li id="4">4</li>
<li id="1">1</li>
</ul>

--->

<ul>
<li id="3">3</li>
<li id="4">4</li>
<li id="1">1</li>
<li id="2">2</li>
</ul>

It will auto change first <li> to the last <li>

I tried, but it is not worked :(

var first = $("ul li:first");
var last = $("ul li:last");

Tks for help

like image 522
PLorida Avatar asked Apr 16 '14 03:04

PLorida


3 Answers

you need to use appendTo('ul')

jsfiddle example

like image 171
ɹɐqʞɐ zoɹǝɟ Avatar answered Oct 16 '22 07:10

ɹɐqʞɐ zoɹǝɟ


The appendTo() method inserts HTML elements at the end of the selected elements.

so you must use this

$('li:first').appendTo('ul');
like image 40
Muhammad Ali Bala Avatar answered Oct 16 '22 06:10

Muhammad Ali Bala


Fiddle

var cont = document.getElementById('container');
cont.appendChild(cont.firstChild);

The beauty of vanilla js.

like image 35
bjb568 Avatar answered Oct 16 '22 08:10

bjb568