So, I have one ASP.Net MVC 5 application. In that one of page hasfour menu items. When page loads first menu item is selected by default (so it should be highlighted as soon as the page loads). And now as soon as user clicks on any other menu, that other menu should be in highlighted stage, so that user knows which menu he is currently on. Below is my attempt:
My thinking:
a. add a click event on the ul tag.
b. when clicked find "active" class inside ul tag and remove it.
c. now add active class to the source of click event (which means the li tag on which user clicked).
Issue:
The selected/clicked menu gets highlighted for a second, and then automatically first menu gets highlighted. So as soon as the whole page corresponding to the newly clicked menu item reloads the first menu item gets highlighted back.
My Attempt
P.S: This is a partial View, And all redirects will load this partial view afresh.
@if (Request.IsAuthenticated)
{
<ul style="list-style-type:none;">
<li class="active setBtnMargin">
<a href="@Url.Action("Index", "Resume")" class="btn btn-block">My Resume </a>
</li>
<li class="setBtnMargin">
<a href="@Url.Action("Index", "CoverLetter")" class="btn btn-block">My Cover Letter </a>
</li>
<li class="setBtnMargin">
<a href="@Url.Action("Index", "Home")" class="btn btn-block">My Account </a>
</li>
<li class="setBtnMargin">
<a href="@Url.Action("Index", "Home")" class="btn btn-block">Get Rewards </a>
</li>
</ul>
}
<script>
// ATTEMPT 1
$("ul").on("click", "li", function () {
$('ul li').removeAttr('active');
$(this).addClass('active');
});
// ATTEMPT 2
//$("li").click(function () {
// //$("ul li").removeClass("active");
// $(this).addClass("active");
//});
</script>
EDIT 1
So the problem is as the page gets redirected. The active tag again gets appened to the first menu item when entire DOM gets reloaded because all the landing pages use the same partial view.