Sorting <li> with jQuery
Currently, I have a choice of language that is simple <ul> with <li> and <a> inside.
Each <li> has a class - lang-inactive or lang-active . It depends on what language the user is using right now.
<li> with .lang-inactive are hidden by default. When you .hover() ul , other options are displayed.


Here is a simple example.
But, as you can see, the first <li> is French, and when I use English and I set up a language bar, French appears above English.
Is there a way to sort <li> depending on whether they are lang-active or lang-inactive . Inactive should appear below active.
My current code is:
var ul = $('#languages-iv'); ul.css('position', 'absolute'); ul.css('top', 5); li = ul.children('li'); li.detach().sort(function(a,b) { //how do I sort }); ul.append(li); $("#languages-iv").hover(function(){ $('.lang-inactive').slideToggle(); }, function() { $('.lang-inactive').stop().slideToggle(); }); +4
3 answers
<ul> <li><a href="#" class="lang-active">English</a></li> <li><a href="#">French</a></li> <li><a href="#">German</a></li> </ul> <script> $(document).ready(function(){ $('ul li').live('click',function(){ $('li a').removeClass('lang-active'); var elem = $(this); $(this).remove(); $('ul').prepend(elem); $(this).children('a').addClass('lang-active'); }); }); <script> +2