Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9047067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:48:42+00:00 2026-06-16T11:48:42+00:00

Trying to create a navigation from an existing menu, here’s the markup: <nav> <ul

  • 0

Trying to create a navigation from an existing menu, here’s the markup:

<nav>
    <ul id="nav">
        <li>
            <a href="?=home">Home</a>
            <ul>
                <li><a href="?=sub-1">Sub 1</a></li>
                <li><a href="?=sub-2">Sub 2</a></li>
                <li><a href="?=sub-3">Sub 3</a></li>
                <li><a href="?=sub-4">Sub 4</a></li>
                <li><a href="?=sub-5">Sub 5</a></li>
            </ul>
        </li>
        <li>
            <a href="?=about">About</a>
        </li>
        <li>
            <a href="?=services">Services</a>
        </li>
        <li>
            <a href="?=portfolio">Portfolio</a>
        </li>
        <li>
            <a href="?=contact">Contact</a>
        </li>
    </ul>
</nav>

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.

var nav = document.getElementById('nav');
    var list = nav.children.length;

    for (var i = 0; i < 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);
    }

So it would basically do this:

<select>
    <option value="?=home">Home</option>
    <option value="?=sub-1">- Sub 1</option>
    <option value="?=sub-2">- Sub 2</option>

Etc. etc…

Any help would be massively appreciated! Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:48:43+00:00Added an answer on June 16, 2026 at 11:48 am

    You can traverse your ul recursively. That will handle multilevel menus as well. Keep track of the recursion level and the higher level items to concatenate with the dashes.

    A description of the approach: http://blog.swapnilsarwe.com/javascript-traversing-html-dom-recursively.html


    Edit: Here’s an example. You’ll also need to put an HTML select element with id=’select’ somewhere:

    buildSelect(document.getElementById('nav'), '', document.getElementById('select'));
    
    function buildSelect(current, optionSoFar, select) {
        var currentLevelOption = optionSoFar;
        for (var i=0; i<current.children.length; i++) {
            var child = current.children[i];
            if ('A' == child.tagName) {
                var currentLevelLabel = child.innerText;
                var separator = ('' == optionSoFar) ? '' : ' - ';
                currentLevelOption = optionSoFar + separator + currentLevelLabel;
                var option = document.createElement('option');
                option.innerText = currentLevelOption;
                option.value = child.href;
                select.appendChild(option);
            }
            else {
                buildSelect(child, currentLevelOption, select);
            }
        }
    }
    

    ​
    ​
    Here’s a fiddle: http://jsfiddle.net/GM54F/3/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'am trying to create a navigation menu with a sub menu, and fiddled with
I'm trying to create a two-level horizontal navigation menu (or menubar) that displays the
I am trying to create a multi level navigation menu with css. I guess
I am trying to figure out the best way to create a navigation menu
I'm currenty trying to create a nice nested navigation menu. The problem I face
I am trying to create a sub navigation. Right now I have two subnav.
I'm currently trying to create a drop-down menu from nested unordered lists. I have
I'm trying to create a navigation menu with menu items that don't always have
I'm having a pretty big problem trying to create navigation on my page. If
I'm trying to create a navigation bar in html+css that looks like the image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.