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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:18:36+00:00 2026-05-13T22:18:36+00:00

I have a navigation bar that has two dropdowns (as nested ul’s). I’m trying

  • 0

I have a navigation bar that has two dropdowns (as nested ul’s). I’m trying to toggle the subnavs when its parent is clicked. Or hide one subnav when the other parent is clicked. Here’s the markup:

<div id="nav-bar">
  <a href="#">A link</a> |
  <ul>
    <li id="feedback">
      <a href="javascript:void(0);">Feedback</a>
      <ul class="subnav">
        <li><a href="#">Give us feedback</a></li>
       </ul>
     </li>
  </ul> |
  <a href="#">Another link</a> |
  <ul>
    <li id="location">
      <a href="javascript:void(0);">Pick your location</a>
      <ul class="subnav">
        <li><a href="#">Los Angeles</a></li>
        <li><a href="#">New York</a></li>
       </ul>
     </li>
  </ul>
</div>

And the code:

//hide the subnavs and give them a little down arrow
$('ul.subnav').hide().parent().append('<small>&#9660;</small>');

// show its subnav when clicked
$('#nav-bar ul li').click(function() {
  var subnav = $(this).children('ul.subnav');
  // hide the other's subnav if it's visible
  if ($(this).attr('id') == 'location') {
    subnav.toggle();
    $('li#feedback').children('ul.subnav').hide();
  } else {
    subnav.toggle();
    $('li#location').children('ul.subnav').hide();
  }
});

Still a novice to JS and jQuery, I’d like to know if there’s a less verbose way to accomplish what I’m trying to do above.

Edit: A better question is, is there a way to do this without having to explicitly give the li’s an id?

  • 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-13T22:18:36+00:00Added an answer on May 13, 2026 at 10:18 pm

    A better format would be something like this for your HTML:

    <ul id="nav-bar">
      <li><a href="#">A link</a> |</li>
      <li>
        <a href="#">Feedback</a> |
        <ul class="subnav">
          <li><a href="#">Give us feedback</a></li>
         </ul>
      </li>
      <li><a href="#">Another link</a> |</li>
      <li>
        <a href="#">Pick your location</a> |
        <ul class="subnav">
          <li><a href="#">Los Angeles</a></li>
          <li><a href="#">New York</a></li>
         </ul>
       </li>
    </ul>
    

    Then your jQuery code would look like this:

    $(function(){
      var $nav    = $("#nav-bar"),
          $subnav = $nav.find("ul.subnav").hide();
    
      $nav.children('li:has(ul.subnav)').click(function(e){
        e.preventDefault();
        var $sub = $(this).children('ul.subnav').toggle();
        $subnav.not($sub).hide();
      })
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to have a horizontal navigation bar that has equally spaced links spanning across
recently I have tried to achieve a navigation bar that has two sides, one
I have a view MainView that has a navigation bar without any navigation controller,
I have a site that has a scrolling navigation bar. I need to save
I have a layout of fixed-pixel width (960px) with a navigation bar that has
My website has a navigation bar that when clicked targets an iframe. i would
I'm trying to select the last element of a navigation bar that could have
I have a Navigation Bar at top that has: <%= link_to content_tag(:li, Watching me),
I have a navigation bar that tells the user which page is selected by
I have a UINavigationBar based application - only a one navigation bar that is

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.