views:

108

answers:

2

hello i have this code

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

i want to make a function that foreach time the function is called the first li at the top will be the last one so the first example will look like this

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

Thanks

+4  A: 
$('li:first').appendTo('ul');

working example (click anywhere): http://jsbin.com/izize3

Kobi
What if there is more than one list?
Gumbo
@Gumbo - then this will not work, it will append the first li to all uls. It needs better selectors, but the idea stays the same, eg `$('#list > li:first').appendTo('#list');` . I might ask: what if we have a ul inside ul? (the question is very broad, to my defence, so I just showed the idea)
Kobi
A: 

Try this:

var firstChild = $("ul > li:first-child");
firstChild.appendTo(firstChild.parent("ul"));
Gumbo