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 9050395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:38:12+00:00 2026-06-16T12:38:12+00:00

Just a quick one, but I’m having some trouble getting my head around it.

  • 0

Just a quick one, but I’m having some trouble getting my head around it.

I’m using javascript to detect elements inside a menu:

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

But the menu has nested <ul> for dropdowns, how do I target these?

  • 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-16T12:38:13+00:00Added an answer on June 16, 2026 at 12:38 pm

    After looking at your code and actually reading and seeing your fiddle I removed my other answer as it was misinterpreting what you need…

    In short you need to write a recursive function to traverse all the child uls when found. I don’t have my development env running so will psuedo code this for you (and I’m sure you’ll get the drift)

    RecursiveFunction(document.getElementById('nav'), "-");
    

    //elsewhere

    function RecursiveFunction(domElement, prependedChar)
    {
    
        //because this is .js you will probably need some closures...
        var currPrependedChar = prependedChar;
        var dom = domElement;
    
        //iterate all nodes, check if they are a li/a and populate dropdown...
        for(count;count < dom.list.Length;++count){
    
        //if the element you have found is a ul recurse
        if(dom.list[count] == "ul"){
             RecursiveFunction(dom.list[count], currPrependedChar + currPrependedChar ); //this calls this function
        }else{
             //combobox option.Value = currPrependedChar + elementInnerText
        }
        }
    }
    

    This is the completed recursive function in fiddle form

    Working fiddle

    CODE

    function RecursiveFunction(currDom, currPrependedChar) {
    
    //because this is .js you will probably need some closures...
    var prependedChar = currPrependedChar;
    var dom = currDom;
    var children = dom.children;
    var childrenCount = children.length;
    
    
    //iterate all nodes, check if they are a li/a and populate dropdown...
    for (var i = 0; i < childrenCount; ++i) {
    
        var curElem = children[i];
        //if the element you have found is a ul recurse
        switch (curElem.nodeName) {
        case "A":
            var option = document.createElement('option');
            option.innerHTML = prependedChar + curElem.text;
            option.value = curElem.href;
            select.appendChild(option);
            break;
            default:
                if(curElem.nodeName == "UL") prependedChar += prependedChar
                RecursiveFunction(curElem, prependedChar);
            break;
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a quick one: what are peoples thoughts on using Action delegates for public
Just a quick one. If I'm using an EJBQL query with named parameters, can
This is just a quick one but I cant find what i need to
Just a quick one about installing a PHP website, are there any tools out
Just a quick one, hopefully. I've coded up this: <script> $(div.design-project).css('display', 'none'); function InsertContent(tid)
just a quick question, was trying different ways to accomplish this, but ended with
Just a quick one - I wrote a php script recently that dynamically creates
just a quick query about safe/unsafe states in Dijkstra's Banker's algorithm ... If one
Just a quick question on drawing quads. I'm currently using: GraphicsDevice.DrawPrimitives(PrimitiveType primitiveType, int startVertex,
Just a quick one, which is fustrating! I have three tabs which just literally

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.