0

I have this responsive collapsable menu.

<nav class="navbar navbar-default">
        <div class="container-fluid col-md-12">
            <div class="col-md-2"></div>
            <div id="logo" class="col-md-3"></div>
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>
            <div class="collapse navbar-collapse col-md-6" id="myNavbar">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#"><span>Home</span></a></li>
                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#"><span>Things</span> <!-- <span class="caret"></span> --></a>
                        <ul class="dropdown-menu">
                            <li><a href="#"><span>Drop1</span></a></li>
                            <li><a href="#"><span>Drop2</span></a></li>
                        </ul>
                    </li>
                    <li><a href="#"><span>Carriers</span></a></li>
                    <li><a href="#"><span>Contacts</span></a></li>
                </ul>
            </div>
        </div>
    </nav>

I added this code to add and remove "active" class for styling purposes:

<script>
        var selector = '.nav li';
        $(selector).on('click', function() {
            $(selector).removeClass('active');
            $(this).addClass('active');
        });
        </script>

Problem is, I cannot style the background color for active menu items. I've tried everything I can think of, and everything I could find, but with no success. I cannot change the bootstrap default color for some reason. Everything else is working as it should, I am able to style other elements, but not this for some reason. Here is a JSFiddle

4 Answers 4

2

Change selector to .nav li a. Because the CSS class active was being set for the li's and your style was for li > a.active.

Also use e.stopPropagation(); to prevent event from bubbling to parent elements.

var selector = '.nav li a';
$(selector).on('click', function() {
  $(selector).removeClass('active');
  $(this).addClass('active');
  e.stopPropagation();
});

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

I am marking your answer as the solution, because your code works in JSfiddle as it should, but on my menu it does not work properly. I guess something else is causing problems, but I don't know what. To describe the issue - when I press a button on the menu, nothing happens, it does not get selected, but when I click elsewhere on the page after that, the background that is desired, is shown. Also, this does not work for the dropdown menu - it stays the default grey colour from bootstrap3. I can upload the nav menu to a hosting and share it, if that would help you help me.
Try CSS li > a.active:focus
It's the same as before
0

This is because you are adding and removing the active class from '.nav li' but in your css you are styling 'li > a.active'. Therefore you need to either change your css to 'li.active' or change your javascript to var selector = .nav li a in order to match your css styling.

Comments

0

You can do this:

var selector = '.nav li';
$(selector).on('click', function(e) {
  $('> a', selector).removeClass('active'); // just remove the class to the anchor
  $('> a', this).addClass('active'); // add the class to current anchor
  
  // $(selector).find('> a') === $('> a', selector)   
  
  e.stopPropagation(); // stop the event to bubbleup
});
li > a.active {
  background-color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="navbar navbar-default">
  <div class="container-fluid col-md-12">
    <div class="col-md-2"></div>
    <div id="logo" class="col-md-3"></div>
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <div class="collapse navbar-collapse col-md-6" id="myNavbar">
      <ul class="nav navbar-nav">
        <li class=""><a href="#"><span>Home</span></a>
        </li>
        <li class="dropdown">
          <a class="dropdown-toggle" data-toggle="dropdown" href="#"><span>Things</span> <!-- <span class="caret"></span> --></a>
          <ul class="dropdown-menu">
            <li><a href="#"><span>Drop1</span></a>
            </li>
            <li><a href="#"><span>Drop2</span></a>
            </li>
          </ul>
        </li>
        <li><a href="#"><span>Carriers</span></a>
        </li>
        <li><a href="#"><span>Contacts</span></a>
        </li>
      </ul>
    </div>
  </div>
</nav>

Comments

0

Look at this fiddle https://jsfiddle.net/oboshto/tcwgvj70/8/

You should use another selector for your query. Also you can use .toggleClass() for adding/removing the class

    var selector = '.nav li a';
    $(selector).on('click', function(e) {
      $(selector).toggleClass('active');
      e.stopPropagation();
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.