3

I want to list all children in DIV by jQuery. But I need order this children by z-index. From lower to highest.

jsFiddle Example

HTML

<div class="container">
    <div style="z-index: 1; ">1b</div>
    <div style="z-index: 10;">10</div>
    <div style="z-index: 3;">3a</div>
    <div style="z-index: 5;">5b</div>
    <div style="z-index: 7;">7</div>
    <div style="z-index: 2;">2</div>
    <div style="z-index: 3;">3b</div>
    <div style="z-index: 5;">5a</div>
    <div style="z-index: 1;">1a</div>
</div>

jQuery

$('.container').children().each(function () {
    alert( $(this).css('z-index') );
});

I need to get ordered output like :

1b
1a
2
3a
3b
5b
5a
7
10
Patrik
  • 1,269
  • 7
  • 30
  • 49

1 Answers1

2

You can use:

function sort_li(a, b){
  return (parseInt(a.style.zIndex,10)) > (parseInt(b.style.zIndex,10)) 
}

$(".container div").sort(sort_li).appendTo('.container');

Working Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125