Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery, move an inner element to the first position?

Lets say you have a list of items:

<ul id="ImportantNumbers">
   <li id="one"></li>
   <li id="two"></li>
   <li id="topNumber"></li>
   <li id="four"></li>
</ul>

Every five seconds these list items get reordered.

Using jquery whats the best way to keep #topNumber, at the top of the list during the reordering.

like image 872
Andrew Avatar asked Aug 17 '10 19:08

Andrew


People also ask

How do I move a DIV from one place to another in jQuery?

All you have to do is select the element(s) you want to move, then call an “adding” method such as append() , appendTo() or prepend() to add the selected elements to another parent element. jQuery automatically realises that the element(s) to add already exist in the page, and it moves the element(s) to the new parent.

What is prepend in jQuery?

The prepend() method inserts specified content at the beginning of the selected elements. Tip: To insert content at the end of the selected elements, use the append() method.

How do I move a div to another div?

Answer: Use the jQuery . appendTo() Method You can use the jQuery . appendTo() method to move an element into another element.


1 Answers

You can use .prependTo() immediately after the re-order, like this:

$("#topNumber").prependTo("#ImportantNumbers"); 

You can see it working here, all it's doing is taking the element and inserting it as the first child on the <ul>, effectively moving it to the top.

Alternatively when you sort, use :not() to exclude it depending on how the sorting works, for example:

$("#ImportantNumbers li:not(#topNumber)").randomize(); 
like image 69
Nick Craver Avatar answered Oct 09 '22 09:10

Nick Craver