javascript - Create Ordered List Based on Div Classes -
i have several div
s siblings , turn these outline looks following:
- one
- two
- three
- one
- one
- two
- two
- one
- four
- five
i'm not quite sure how this. i've had luck following codepen. i'm using css classes of 1 through ten signify how deep need indent list.
i accomplish using javascript.
use stack ... (sorry using coffee logic matters)
entrylevel = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ] #make class list bit tricky all_items="."+entrylevel.join(", .") #this find index / level of each element (could have more on class) get_index = ($$) => cla, in entrylevel return i+1 if $$.hasclass(cla)
here go base element body
(or ever want) , need remember things
stack=[$('body')] old_index=1 list_index=1
we iterate on classes , index
$(all_items).each (i,item) => $$=$(item) i=get_index($$)
we have remove topmost container if go level - how remember old index (here missed "()" after pop
stack.pop() if i<old_index old_index=i
lets take if have list container, if not append new list parent ...
unless stack[i] id="list_#{list_index}" list_index+=1 stack[i-1].append("<ol id='#{id}'></ol>") stack[i]=$("#"+id)
finally move item in topmost list
stack[i].append($$)
or list:
$$.wrap("<li></li>") stack[i].append($$.parent())
Comments
Post a Comment