0

How can I change the order of both lists, while preventing to mix them up. I know how to do it with one list, but get stuck when I got several..

Fiddle

HTML:

<ul>
    <li>A</li>
    <li>B</li>
</ul>
<ul>
    <li>C</li>
    <li>D</li>
</ul>
<a href="#" class="switch_order">Switch</a>

jQuery

$('body').on('click', '.switch_order', function(e){
  var list = $('ul');
  var listItems = list.children('li');
  list.append(listItems.get().reverse());
});
Pepijn Gieles
  • 619
  • 14
  • 23

3 Answers3

1
$('body').on('click', '.switch_order', function(e){
 $('ul').each(function(){
  var listItems = $(this).children('li');
  $(this).append(listItems.get().reverse());
 });
});

Updated JSFiddle

T J
  • 42,762
  • 13
  • 83
  • 138
1

You can use:

$('body').on('click', '.switch_order', function(e){
    $('ul li').each(function(i,li){
        $(this).parent().prepend(li);
    })
});

Fiddle Demo

Felix
  • 37,892
  • 8
  • 43
  • 55
0

This is one way

All I did here was give an id to each <ul> and targeted them seperately
lennon626
  • 124
  • 6