0

I want to change background color of <li> where the class is active, when I click on those li > a. How can I do it in this case. He is what I have tried:

Script

<script>
    $('.sidebar-collapse .nav > li > a').click(function (e) {
        var $this = $(this);
        if ($this.hasClass('active')) {
            $this.removeAttr('background');
        }
    });
</script>

Style

.sidebar-collapse .nav > li > a {
    background: #EFEFEF;
    text-shadow: none;
    color: #595959;
    border-top: 1px solid #dfdfdf;
}

Menu List HTML

<nav class="navbar-default navbar-side" role="navigation">
    <div class="sidebar-collapse">
        <ul class="nav" id="main-menu">
            <li>
                <a href="default.aspx"><i class="fa fa-desktop "></i>Dashboard</a>
            </li>
            <li>
                <a href="#"><i class="fa fa-edit "></i>UI Elements<span class="fa arrow"></span></a>
                <ul class="nav nav-second-level">
                    <li>
                        <a href="#">Notifications</a>
                    </li>
                    <li>
                        <a href="#">Elements</a>
                    </li>
                    <li>
                        <a href="#">Free Link</a>
                    </li>
                </ul>
            </li>
            <li>
                <a href="#"><i class="fa fa-table "></i>Table Examples</a>
            </li>
            <li>
                <a href="#"><i class="fa fa-edit "></i>Forms </a>
            </li>
            <li>
                <a href="#"><i class="fa fa-sitemap "></i>Multi-Level Dropdown<span class="fa arrow"></span></a>
                <ul class="nav nav-second-level">
                    <li>
                        <a href="#">Second Level Link</a>
                    </li>
                    <li>
                        <a href="#">Second Level Link</a>
                    </li>
                    <li>
                        <a href="#">Second Level Link<span class="fa arrow"></span></a>
                        <ul class="nav nav-third-level">
                            <li>
                                <a href="#">Third Level Link</a>
                            </li>
                            <li>
                                <a href="#">Third Level Link</a>
                            </li>
                            <li>
                                <a href="#">Third Level Link</a>
                            </li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li>
                <a href="#"><i class="fa fa-qrcode "></i>Tabs & Panels</a>
            </li>
            <li>
                <a href="#"><i class="fa fa-bar-chart-o"></i>Mettis Charts</a>
            </li>

            <li>
                <a href="#"><i class="fa fa-edit "></i>Last Link </a>
            </li>
            <li>
                <a href="blank.aspx"><i class="fa fa-table "></i>Blank Page</a>
            </li>
        </ul>
    </div>
</nav>
8
  • you are looking for the :active pseudo selector Commented Jul 21, 2014 at 8:32
  • 1
    This is jquery not js. Commented Jul 21, 2014 at 8:32
  • i let to change code wait a minute Commented Jul 21, 2014 at 8:34
  • 1
    @Alek Like if jQuery is not javascript... :) Commented Jul 21, 2014 at 8:37
  • 2
    @user3001046 Why not pure CSS? .sidebar-collapse .nav > li > a.active {background-color: transparent;} Commented Jul 21, 2014 at 8:39

1 Answer 1

1

If you are somewhy bounded to js solution you need to use css. Since background is not an attribute but a style and can't be "removed".

$('.sidebar-collapse .nav > li > a').click(function (e) {
    var li = $(this).closest('li');
    if (li.hasClass('active')) {
        li.css('background-color', 'transparent'); //or inherit
    }
});

Or a pure CSS solution. Which is preferred:

.sidebar-collapse .nav > li.active > a {
    background-color: transparent;
}
Sign up to request clarification or add additional context in comments.

1 Comment

You will change like this .sidebar-collapse .nav > li.active > a { background-color: transparent; }

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.