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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:55:43+00:00 2026-06-08T13:55:43+00:00

I am trying to work out how to change between this: <li class=menu> and

  • 0

I am trying to work out how to change between this:

            <li class="menu">

and this:

            <li class="menu open">

on each click of the menu. Basically so the menu opens on each click if closed and closes on each click if open

Code Snippet:

<div id="headerbar">
<div class="topbar">
  <div class="fill">
    <div class="container">

      <ul>
            <li><a href="./index.php"><img src="./img/home_icon.png" alt="Home"></a></li>
      </ul>



      <ul>
        <li class="menu">
          <a href="#" class="menu">Menu Heading</a>
        <ul class="menu-dropdown">
            <li><a href="http://google.com">Option 1</a></li>
            <li><a href="http://google.com">Option 2</a></li>
            <li><a href="http://google.com">Option 3</a></li>
            <li><a href="http://google.com">Option 4</a></li>
          </ul>
        </li>
      </ul>

    </div>
  </div>
</div>
</div>

I have read a few posts about toggleClass but cannot seem to get it working. If someone could break it down simply for me it would be greatly appreciated!

Here is a clipping from the CSS:

.topbar div > ul a.menu:hover,
.nav a.menu:hover,
.topbar div > ul li.open .menu,
.nav li.open .menu,
.topbar div > ul .dropdown-toggle:hover,
.nav .dropdown-toggle:hover,
.topbar div > ul .dropdown.open .dropdown-toggle,
.nav .dropdown.open .dropdown-toggle {
  background: #444;
  background: rgba(255, 255, 255, 0.05);
}
.topbar div > ul .menu-dropdown,
.nav .menu-dropdown,
.topbar div > ul .dropdown-menu,
.nav .dropdown-menu {
  background-color: #333;
}
.topbar div > ul .menu-dropdown a.menu,
.nav .menu-dropdown a.menu,
.topbar div > ul .dropdown-menu a.menu,
.nav .dropdown-menu a.menu,
.topbar div > ul .menu-dropdown .dropdown-toggle,
.nav .menu-dropdown .dropdown-toggle,
.topbar div > ul .dropdown-menu .dropdown-toggle,
.nav .dropdown-menu .dropdown-toggle {
  color: #ffffff;
}
.topbar div > ul .menu-dropdown a.menu.open,
.nav .menu-dropdown a.menu.open,
.topbar div > ul .dropdown-menu a.menu.open,
.nav .dropdown-menu a.menu.open,
.topbar div > ul .menu-dropdown .dropdown-toggle.open,
.nav .menu-dropdown .dropdown-toggle.open,
.topbar div > ul .dropdown-menu .dropdown-toggle.open,
.nav .dropdown-menu .dropdown-toggle.open {
  background: #444;
  background: rgba(255, 255, 255, 0.05);
}
.topbar div > ul .menu-dropdown li a,
.nav .menu-dropdown li a,
.topbar div > ul .dropdown-menu li a,
.nav .dropdown-menu li a {
  color: #999;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.5);
}
.topbar div > ul .menu-dropdown li a:hover,
.nav .menu-dropdown li a:hover,
.topbar div > ul .dropdown-menu li a:hover,
.nav .dropdown-menu li a:hover {
  background-color: #191919;
  background-repeat: repeat-x;
  background-image: -khtml-gradient(linear, left top, left bottom, from(#292929),     to(#191919));
  background-image: -moz-linear-gradient(top, #292929, #191919);
  background-image: -ms-linear-gradient(top, #292929, #191919);
  background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #292929), color-stop(100%, #191919));
  background-image: -webkit-linear-gradient(top, #292929, #191919);
  background-image: -o-linear-gradient(top, #292929, #191919);
  background-image: linear-gradient(top, #292929, #191919);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#292929', endColorstr='#191919', GradientType=0);
  color: #ffffff;
}
.topbar div > ul .menu-dropdown .active a,
.nav .menu-dropdown .active a,
.topbar div > ul .dropdown-menu .active a,
.nav .dropdown-menu .active a {
  color: #ffffff;
}
.topbar div > ul .menu-dropdown .divider,
.nav .menu-dropdown .divider,
.topbar div > ul .dropdown-menu .divider,
.nav .dropdown-menu .divider {
  background-color: #222;
  border-color: #444;
}
.topbar ul .menu-dropdown li a, .topbar ul .dropdown-menu li a {
  padding: 4px 15px;
}
li.menu, .dropdown {
  position: relative;
}
a.menu:after, .dropdown-toggle:after {
  width: 0;
  height: 0;
  display: inline-block;
  content: "&darr;";
  text-indent: -99999px;
  vertical-align: top;
  margin-top: 8px;
  margin-left: 4px;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 4px solid #ffffff;
  filter: alpha(opacity=50);
  -khtml-opacity: 0.5;
  -moz-opacity: 0.5;
  opacity: 0.5;
}
.menu-dropdown, .dropdown-menu {
  background-color: #ffffff;
  float: left;
  display: none;
  position: absolute;
  top: 40px;
  z-index: 900;
  min-width: 160px;
  max-width: 220px;
  width: 160px;
  margin-left: 0;
  margin-right: 0;
  padding: 6px 0;
  zoom: 1;
  border-color: #999;
  border-color: rgba(0, 0, 0, 0.2);
  border-style: solid;
  border-width: 0 1px 1px;
  -webkit-border-radius: 0 0 6px 6px;
  -moz-border-radius: 0 0 6px 6px;
  border-radius: 0 0 6px 6px;
  -webkit-box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  -moz-box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  -webkit-background-clip: padding-box;
  -moz-background-clip: padding-box;
  background-clip: padding-box;
}
.menu-dropdown li, .dropdown-menu li {
  float: none;
  display: block;
  background-color: none;
}
.menu-dropdown .divider, .dropdown-menu .divider {
  height: 1px;
  margin: 5px 0;
  overflow: hidden;
  background-color: #eee;
  border-bottom: 1px solid #ffffff;
}
.topbar .dropdown-menu a, .dropdown-menu a {
  display: block;
  padding: 4px 15px;
  clear: both;
  font-weight: normal;
  line-height: 18px;
  color: #808080;
  text-shadow: 0 1px 0 #ffffff;
}
.topbar .dropdown-menu a:hover,
.dropdown-menu a:hover,
.topbar .dropdown-menu a.hover,
.dropdown-menu a.hover {
  background-color: #dddddd;
  background-repeat: repeat-x;
  background-image: -khtml-gradient(linear, left top, left bottom, from(#eeeeee), to(#dddddd));
  background-image: -moz-linear-gradient(top, #eeeeee, #dddddd);
  background-image: -ms-linear-gradient(top, #eeeeee, #dddddd);
  background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #eeeeee), color-stop(100%, #dddddd));
  background-image: -webkit-linear-gradient(top, #eeeeee, #dddddd);
  background-image: -o-linear-gradient(top, #eeeeee, #dddddd);
  background-image: linear-gradient(top, #eeeeee, #dddddd);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#eeeeee', endColorstr='#dddddd', GradientType=0);
  color: #404040;
  text-decoration: none;
  -webkit-box-shadow: inset 0 1px 0 rgba(0, 0, 0, 0.025), inset 0 -1px rgba(0, 0, 0, 0.025);
  -moz-box-shadow: inset 0 1px 0 rgba(0, 0, 0, 0.025), inset 0 -1px rgba(0, 0, 0, 0.025);
  box-shadow: inset 0 1px 0 rgba(0, 0, 0, 0.025), inset 0 -1px rgba(0, 0, 0, 0.025);
}
.open .menu,
.dropdown.open .menu,
.open .dropdown-toggle,
.dropdown.open .dropdown-toggle {
  color: #ffffff;
  background: #ccc;
  background: rgba(0, 0, 0, 0.3);
}
.open .menu-dropdown,
.dropdown.open .menu-dropdown,
.open .dropdown-menu,
.dropdown.open .dropdown-menu {
  display: block;
}
  • 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-08T13:55:45+00:00Added an answer on June 8, 2026 at 1:55 pm

    If you aren’t using jQuery and need to do this in plain javascript, then you can do it like this:

    // function called by click to toggle class on parent
    function toggleMenu(item) {
        toggleClass(item.parentNode, "open");
        return(false);    // don't do other default handling for the click
    }
    
    // utility functions for adding, removing classes
    function addClass(elem, cls) {
        var oldCls = elem.className;
        if (oldCls) {
            oldCls += " ";
        }
        elem.className = oldCls + cls;
    }
    
    function removeClass(elem, cls) {
        var str = " " + elem.className + " ";
        elem.className = str.replace(" " + cls + " ", "").replace(/^\s+/g, "").replace(/\s+$/g, "");
    }
    
    function hasClass(elem, cls) {
        var str = " " + elem.className + " ";
        var testCls = " " + cls + " ";
        return(str.indexOf(testCls) != -1) ;
    }
    
    function toggleClass(elem, cls) {
        if (hasClass(elem, cls)) {
            removeClass(elem, cls);
        } else {
            addClass(elem, cls);
        }
    }
    

    And, in your HTML, I added an onclick handler to one item:

     <ul>
        <li class="menu">
          <a href="#" onclick="toggleMenu(this)">Menu Heading</a>
        <ul class="menu-dropdown">
            <li><a href="http://google.com">Option 1</a></li>
            <li><a href="http://google.com">Option 2</a></li>
            <li><a href="http://google.com">Option 3</a></li>
            <li><a href="http://google.com">Option 4</a></li>
          </ul>
        </li>
      </ul>
    

    Working demo: http://jsfiddle.net/jfriend00/2pVmF/

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

Sidebar

Related Questions

I'm trying to work out what regular expression I would need to change this
I am trying to work out a way to change between two sets of
I am trying work out with MERGE statment to Insert / Update Dimension Table
Im trying to work out how to get the value from one setting in
I trying to work out what regex will match filenames in quotes. Eg. blah
i am trying to work out the following problem in Java (although it could
I am trying to work out why I can not access the nested function
I am trying to work out how I can insert the string End into
I am trying to work out the best practice for my problem. I have
I am trying to work out an IF statement and getting lost. I am

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.