Note that there are some explanatory texts on larger screens.

plurals
  1. POFind and append nested <ul> to select menu with hyphens
    primarykey
    data
    text
    <p>Trying to create a navigation from an existing menu, here's the markup:</p> <pre><code>&lt;nav&gt; &lt;ul id="nav"&gt; &lt;li&gt; &lt;a href="?=home"&gt;Home&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="?=sub-1"&gt;Sub 1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="?=sub-2"&gt;Sub 2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="?=sub-3"&gt;Sub 3&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="?=sub-4"&gt;Sub 4&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="?=sub-5"&gt;Sub 5&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt; &lt;a href="?=about"&gt;About&lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a href="?=services"&gt;Services&lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a href="?=portfolio"&gt;Portfolio&lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a href="?=contact"&gt;Contact&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/nav&gt; </code></pre> <p>This is the javascript loop I have, which currently finds and appends each top level menu item, but I really am unsure how to target the nested children and append them with a hyphen for nested elements.</p> <pre><code>var nav = document.getElementById('nav'); var list = nav.children.length; for (var i = 0; i &lt; list; i++) { var option = document.createElement('option'); option.innerHTML = nav.children[i].children[0].innerHTML; option.value = nav.children[i].children[0].href; select.appendChild(option); } </code></pre> <p>So it would basically do this:</p> <pre><code>&lt;select&gt; &lt;option value="?=home"&gt;Home&lt;/option&gt; &lt;option value="?=sub-1"&gt;- Sub 1&lt;/option&gt; &lt;option value="?=sub-2"&gt;- Sub 2&lt;/option&gt; </code></pre> <p>Etc. etc...</p> <p>Any help would be massively appreciated! Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. 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