tbeseda (owner)

Revisions

gist: 82412 Download_button fork
public
Public Clone URL: git://gist.github.com/82412.git
Embed All Files: show embed
HTML #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!-- The code I'm stuck with -->
<ul>
<li class="level1">textytext</li>
<li class="level2">2textytext</li>
<li class="level2">2textytext</li>
<li class="level1">textytext</li>
<li class="level1">textytext</li>
<li class="level2">2textytext</li>
<li class="level3">3textytext</li>
<li class="level3">3textytext</li>
<li class="level2">2text</li>
<li class="level1">textytext</li>
</ul>
<!-- My attempt moves the last .level2 li up with the others -->
<script type="text/javascript">
    $(document).ready(function() {
        $(".level2").wrapAll("<ul class=level2></ul>");
    });
</script>
<!-- How I want it to look -->
<ul>
<li class="level1">textytext</li>
<ul>
<li class="level2">2textytext</li>
<li class="level2">2textytext</li>
</ul>
<li class="level1">textytext</li>
<li class="level1">textytext</li>
<ul>
<li class="level2">2textytext</li>
<ul>
<li class="level3">3textytext</li>
<li class="level3">3textytext</li>
</ul>
<li class="level2">2text</li>
</ul>
<li class="level1">textytext</li>
</ul>
 
<!-- SUGGESTION FROM zidoh IN #jquery -->
<script>
var curlvl = 1;
var tmp = '';
function get_level($element) {
return $element.attr("class").charAt(5);
}
$(document).ready(function () {
$lis = $("#heh").children();
$lis.each(function () {
var lvl = get_level($(this));
if (lvl != 1) {
if (lvl > curlvl) {
tmp += '<ul>';
}
else if (lvl < curlvl) {
tmp += '</ul>';
}
tmp += '<li class="'+$(this).attr("class")+'">'+$(this).html()+'</li>\n';
$(this).remove();
}
else {
if (curlvl > lvl) {
$(tmp).insertBefore($(this));
tmp = '';
}
}
curlvl = lvl;
});
});
</script>