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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:45:17+00:00 2026-06-14T00:45:17+00:00

Okay guys. What i’m trying to do is create an unordered list (ul) with

  • 0

Okay guys. What i’m trying to do is create an unordered list (ul) with nested (ul)’s inside of it. I’m trying to modify the classes within the elements of the main (ul)’s and i have no idea how to do this. Let me give you an example. Let’s say i have this:

<ul id="mainNav">
<li><a href="#">Main Item 1</a></li>
<li><a href="#">Main Item 2</a>
   <ul>
    <li><a href="#">subitem1</a></li>
    <li><a href="#">subitem2</a></li>
   </ul>
</li>
<li><a href="#">Main Item 3</a></li>
<li><a href="#">Main Item 4</a>
   <ul>
    <li><a href="#">subitem3</a></li>
    <li><a href="#">subitem4</a></li>
   </ul>
</li>
</ul>

What i’m trying to do is find the nested (ul)’s in the “mainNav” and apply with jquery a class to them. Further more, i need to apply a class and other attributes to the parent (li)’s of the nested (ul)’s. I hope this makes sense. The output would look something like this:

<ul id="mainNav">
<li><a href="#">Main Item 1</a></li>
<li class="dropdown"><a href="#" DATA="BLAH">Main Item 2</a> <!-- STUFF GOES HERE -->
   <ul class="dropdown-submenu"> <!-- and here -->
    <li><a href="#">subitem1</a></li>
    <li><a href="#">subitem2</a></li>
   </ul>
</li>
<li><a href="#">Main Item 3</a></li>
<li class="dropdown"><a href="#" DATA="BLAH">>Main Item 4</a>
   <ul class="dropdown-submenu">
    <li><a href="#">subitem3</a></li>
    <li><a href="#">subitem4</a></li>
   </ul>
</li>
</ul>

I hope this makes sense. I’m trying to traverse the child elements and add classes to the child elements (ul)’s and then go to the parent element (li) and add classes to that and the (a) elements as well. I’m completely lost and if you guys could help, i would greatly appreciate it!!! Thanks all!!!

  • 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-14T00:45:19+00:00Added an answer on June 14, 2026 at 12:45 am

    That’s fairly straightforward to do with a single jQuery line:

    $('#mainNav ul').addClass('dropdown-submenu').parent('li').addClass('dropdown');

    Note that using the css selectors, we’re just finding any ul elements of the mainNav, then adding a class, then we chain another function to find it’s parent, and add a class off dropdown.

    EDIT :
    To add the data attribute to the a tag per the example in the question, chain in the find('a:first').attr('data', 'BLAH') bit like so:

    $('#mainNav ul').addClass('dropdown-submenu').parent('li').addClass('dropdown').find('a:first').attr("data", "BLAH");
    

    EDIT 2 :
    To address the next level of ul children, and presumably NOT have all child ul’s have the same class, you can use the following script:

    $('#mainNav > li > ul').addClass('dropdown-menu').parent('li').addClass('dropdown').find('a:first').attr('data-toggle', 'dropdown').addClass('dropdown-toggle');  
    $('#mainNav ul ul').addClass('dropdown-child-menu').parent('li').addClass('dropdown-child');`   
    

    Note the change in selector for the first line: this ensures we only get the ul‘s that are direct descendants of the top-level li elements.

    The second line selector tells it to get all third level+ ul elements (it will get all third, fourth, etc. level ul elements).

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

Sidebar

Related Questions

Okay, guys i have been trying to define a Stack , each node also
Okay guys I have a question about positioning tables within DIV's using CSS. I
Okay guys, I have read through all the other posts and question on jQuery
Okay guys, I am working hashmap, using one of my classes as Key. My
Guys, I have a windows form with a panel control and inside the panel
Okay, I have a question guys. I want to remote upload (copy an image
Okay guys I have a database I have been working on for days now.
Okay, I have a feeling that you guys'll be able to quickly point out
Hye guys. Okay. I have done this coding. But it seems have error. Can
Hey guys. I have some difficulty in creating a filename. okay, here is what

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.