What you do wrong is aimed at the hyperlink, while you need to select only the list item.
But now, if you correct your code to target a list of items in the list instead of hyperlinks, you will not be able to see the changes on the screen. (You might see classes switching in the browser developer tools, though, obviously).
Why is that? Since the hyperlink inside the list item hides all the changes that you want to see when you click on the list item.
I added another CSS property to .highlightMenu so you notice the changes.
See for yourself:
- JavaScript modified to target list items, not hyperlinks in
ul in #navcontainer .highlightMenu contains one additional CSS property (outline) to notice style changes in the click event.
$(document).ready(function() { $('#navcontainer ul li').click(function() { $('.highlightMenu').removeClass('highlightMenu'); $(this).addClass('highlightMenu'); }); });
#navcontainer ul { display: block; list-style-type: disc; padding-top: 40px; -webkit-margin-before: 1em; -webkit-margin-after: 1em; -webkit-margin-start: 0px; -webkit-margin-end: 0px; -webkit-padding-start: 40px; } #navcontainer ul li { display: inline-block; border: 5px solid #009ddc; border-left: 5px solid #009ddc; border-right: 5px solid #009ddc; border-bottom: 5px solid #009ddc; border-top: 5px solid #009ddc; z-index: 0 !important; padding: 0; background: #fff; color: #24387f !important; } #navcontainer li a:hover { color: #fff !important; background-color: #009ddc; } #navcontainer ul li a { text-decoration: none; padding: .2em 3em 1em 1em; color: #24387f !important; font-size: larger; font-weight: bold; } .highlightMenu { color: #fff !important; background-color: #009ddc; outline: 1px solid red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="navcontainer"> <ul class="nav navbar-nav navbar-left text-center"> <li>@Html.ActionLink("Team Management", "Team", "Admin", null, null)</li> <li>@Html.ActionLink("User Management", "UserProfile", "Admin", null, null)</li> </ul> </div>
I hope this helps.
Rahul arora
source share