SleBluue SleBluue - 3 months ago 9
CSS Question

Remove Active Class on second click

Right now I have a menu/sub menus that I am adding active classes to on click. I would like to remove the active class if I click the same menu Item twice. My initial thought was to just toggle the active class in jQuery but that does not seem to work. Any thoughts?? Thank you.



$(document).ready(function() {
$("#menusomething li a").on("click", function() {
$("#menusomething li a").removeClass("active");
$(this).toggleClass("active");
});
});

#menusomething li a{color :green}
#menusomething li a.active{color :red}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="menusomething" style="padding-left:30px">
<li><a href="#">HOME</a></li>
<li><a href="#">ABOUT</a></li>
<li><a class="target" href="#">CHAPTERS</a>
<ul class="submenu">
<li><a href="#">Dallas</a></li>
<li><a href="#">Los Angeles</a></li>
<li><a href="#">New York</a></li>
<li><a href="#">Northern California</a></li>
<li><a href="#">Orange County</a></li>
<li><a href="#">Phoenix</a></li>
<li><a href="#">San Diego</a></li>
<li><a href="#">Washington DC</a></li>
</ul></li>
<li><a href="#">MEMBER SERVICES</a>
<ul class="submenu">
<li><a href="#">Service</a></li>
<li><a href="#">Service2</a></li>
<li><a href="#">Service3</a></li>
<li><a href="#">Service4</a></li>
</ul></li>
<li><a href="#">MENU ITEM</a></li>
<li><a href="#">MENU ITEM 2</a></li>
<li><a href="#">CONTACT</a></li>
</ul>




Answer

Just remove this line:

        $("#menusomething li a").removeClass("active");

as together with the toggle it will make the class to be added again, and so you can never actually remove it.

This will work:

$(document).ready(function() {
    $("#menusomething li a").on("click", function() {
        $(this).toggleClass("active");
    });
});

$(document).ready(function() {
    $("#menusomething li a").on("click", function() {
        $(this).toggleClass("active");
    });
});
#menusomething li a{color :green}
#menusomething li a.active{color :red}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="menusomething" style="padding-left:30px">
  <li><a href="#">HOME</a></li>
  <li><a href="#">ABOUT</a></li>
  <li><a class="target" href="#">CHAPTERS</a>
	<ul class="submenu">
    	<li><a href="#">Dallas</a></li>
        <li><a href="#">Los Angeles</a></li>
        <li><a href="#">New York</a></li>
        <li><a href="#">Northern California</a></li>
        <li><a href="#">Orange County</a></li>
        <li><a href="#">Phoenix</a></li>
        <li><a href="#">San Diego</a></li>
        <li><a href="#">Washington DC</a></li>
    </ul></li>
  <li><a href="#">MEMBER SERVICES</a>
  	<ul class="submenu">
    	<li><a href="#">Service</a></li>
        <li><a href="#">Service2</a></li>
        <li><a href="#">Service3</a></li>
        <li><a href="#">Service4</a></li>
    </ul></li>
  <li><a href="#">MENU ITEM</a></li>
  <li><a href="#">MENU ITEM 2</a></li>
  <li><a href="#">CONTACT</a></li>
  </ul>