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

  • SEARCH
  • Home
  • 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 7665409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:33:29+00:00 2026-05-31T14:33:29+00:00

Ok what I am trying to do is create a dynamic menu based on

  • 0

Ok what I am trying to do is create a dynamic menu based on an array
First dimesion elements will be top level and Second dimension levels will be sub items of said parent how will I do this?

$(document).ready(function(e) {
//var maxLevels = 3;//Max Levels Of The List TO Traverse

var nav = new Array();
nav[0] = "Item A";
nav[0][0] = "Item AB"; 
nav[1] = "Item B";
nav[1][0] = "Item BA";

var menu = "";
nav.forEach(function(e) {
    menu += "<li>" + e + "</li>";
});


$('#info').html(menu);


});
  • 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-05-31T14:33:30+00:00Added an answer on May 31, 2026 at 2:33 pm

    Having declared your array nav you can access its elements with nav[index] but you can’t add a second dimension to item 0 just by saying nav[0][0] – you need to declare nav[0] as an array before using it:

    nav[0] = [];
    nav[0][0] = ...
    

    But you can declare it all in one statement with an array literal, where for your purpose I’d suggest an array of objects:

    var nav = [
        { "title" : "Item A",
          "submenu" : [ "Item A1", "Item A2" ] },
        { "title" : "Item B",
          "submenu" : [ "Item B1", "Item B2", "Item B3"] }
    ];
    

    If your desired output was this:

    <ul>
       <li>Item A
           <ul>
               <li>Item A1</li>
               <li>Item A2</li>
           </ul>
       </li>
       <li>Item B
           <ul>
               <li>Item B1</li>
               <li>Item B2</li>
               <li>Item B3</li>
           </ul>
       </li>
    </ul>
    

    You could do it like so:

    var $menu = $("<ul></ul>");
    $.each(nav, function(i, val) {
        var $item = $("<li></li>").html(val.title),
            $sub = $("<ul></ul>");
        $.each(val.submenu, function(j, sub) {
            $sub.append($("<li></li>").html(sub));
        });
        $menu.append($item.append($sub));    
    });
    $("body").append($menu);
    

    Demo: http://jsfiddle.net/9kRs3/

    Of course you could build the menu up as a string like in your original code, but I’ve chosen to create the elements as I go.

    Using the object structure above you can easily add some extra properties to define what happens when each item is clicked. And you could make each item in the submenu arrays have the same object structure as the top level so that you can nest more and more levels as needed.

    • 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 Dynamic Dropdown menu class which contains a class
I am trying to create a dynamic menu by reading an XML file using
I am trying to create a dynamic menu with simple navigation. The problem is
I'm trying to create a dynamic drop down menu that pulls the city name
I am trying to create a dynamic table that is being built based on
I'm trying to create a dynamic icon menu for my android application. The icon
I'm trying to create a dynamic list that will show item of my city
I'm trying to create a dynamic survey form based on dynamic data by using
I am trying to create dynamic menus from the database using the following example
I am trying to create a dynamic pagination system for my website and I've

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.