javascript - Create Ordered List Based on Div Classes -


i have several divs siblings , turn these outline looks following:

  1. one
  2. two
  3. three
    1. one
      1. one
      2. two
    2. two
  4. four
  5. 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()) 

pen


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -