Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, you should be able to use the dashes, provided that the level is always the number of dashes divided by three. The key is to use a stack. It also helps to have a function to count the dashes.</p> <pre><code>// Helper function. function countLeadingChars(str, ch) { for (var i = 0; i &lt; str.length; i++) { if (str.charAt(i) !== ch) { return i; } } return str.length; } var stack = []; $('#Folder').children().each(function() { var $option = $(this); var text = $option.text(); // Count the dashes. var dashes = countLeadingChars(text, '-'); // Level is # of dashes divided by 3. var level = dashes / 3; // Remove anything in the stack beyond the current level. // This will cause new &lt;ul&gt;s to get created when higher // levels are encountered again. if (stack.length &gt; (level + 1)) { stack.length = level + 1; } // Get the &lt;ul&gt; for the level, creating it if it doesn't exist. var $ul = stack[level]; if (!$ul) { $ul = $('&lt;ul/&gt;'); stack[level] = $ul; // Add the &lt;ul&gt; to the last &lt;li&gt; of the parent &lt;ul&gt;. if (level &gt; 0) { stack[level - 1].children(':last').append($ul); // Add the "class" to the &lt;ul&gt;. var parentText = $ul.parent().text(); var index = parentText.lastIndexOf(' '); $ul.addClass('something' + parentText.slice(index + 1)); } } // Create the &lt;li&gt; and add it to the &lt;ul&gt;. $('&lt;li&gt;' + text.slice(dashes) + '&lt;/li&gt;').appendTo($ul); }); // At this point, stack[0] is the top-most &lt;ul&gt;. </code></pre> <p><a href="http://jsfiddle.net/hspVd/" rel="nofollow">Live Demo on jsfiddle</a></p> <p><strong>EDIT:</strong> I just noticed you wanted a "class" on the <code>&lt;ul&gt;</code> elements I added the code for that above.</p> <p><a href="http://jsfiddle.net/hspVd/2/" rel="nofollow">New Live Demo on jsfiddle</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload