I would like to use a jquery 'click' event to change the class of the selected tab. The first tab is initially listed as selected. When I change tabs, I try to step into the click event but it is skipped. It reaches the function and then just skips over it.
My project is MVC4 and my jquery library is 1.11.3.
This is my markup:
<header>
<div class="content-wrapper">
<p class="site-title">
<img src="~/Images/logo.png" alt="" />
</p>
<nav>
<ul id="menu" class="maintablist">
<li class="selected">@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
</nav>
</div>
</header>
This is my jquery function:
<script src="~/Scripts/jquery-1.11.3.js"></script>
<script>
$("#menu li").click(function () {
$("#menu li").removeClass("selected");
$(this).addClass("selected");
});
</script>
This is my style sheet stuff:
.maintablist {
list-style:none;
height: 2em;
padding: 0;
margin: 0;
border: none;
}
.maintablist li {
float: left;
margin-right: 0.13em;
}
.maintablist li a {
display:block;
padding:0 1em;
text-decoration:none;
border:0.06em solid #000;
border-bottom:0;
font:bold 0.88em/2em arial,geneva,helvetica,sans-serif;
color:#000;
background-color:#ccc;
/* CSS 3 elements */
-webkit-border-top-right-radius:0.50em;
-webkit-border-top-left-radius:0.50em;
-moz-border-radius-topright:0.50em;
-moz-border-radius-topleft:0.50em;
border-top-right-radius:0.50em;
border-top-left-radius:0.50em;
}
.maintablist li a:hover {
background:#b5dbb6;
color:#fff;
text-decoration:none;
}
.maintablist li.selected a {
background-color: #b5dbb6;
color: #fff;
}
Why isn't the click event recognized? I would like to do this on the client side and not have to go to the HomeController to do this. Thanks.
UPDATE Removed the 'click' event from within the 'ready' function and the class is removed. But the class is not added to my current selected tab. This is the code:
<script>
$(".maintablist li").removeClass("selected");
$(this).addClass("selected");
</script>
UPDATE Using this jquery function below, when a tab is clicked it removes the class attribute from all of the tabs. Then the clicked tab is displayed, the initial tab has the 'selected' coloring. All of the tabs should not have a class attribute defined but somehow the class attribute is reapplied to the initial tab yet the selected tab is displayed.
$(document).ready(function () {
$("#mainmenu li").click(function () {
$("#mainmenu li").removeClass("selected").removeAttr("class");
});
});
UPDATE I only posted the information that I believe was needed. But below is the code in the file, _Layout.cshtml
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title OnCall Schedule Tool</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
<link rel="stylesheet" href="~/Content/jquery-ui.css">
<link rel='stylesheet' href='~/Content/fullcalendar.css' />
<link rel='stylesheet' href='~/Content/spectrum.css'>
<script src="~/Scripts/jquery-1.11.3.js"></script>
<script src='~/Scripts/jquery.min.js'></script>
<script src="~/Scripts/jquery-ui.js"></script>
<script src='~/Scripts/moment.min.js'></script>
<script src='~/Scripts/fullcalendar.js'></script>
<script src="~/Scripts/spectrum.js"></script>
<script src="~/Scripts/knockout-2.2.0.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#mainmenu li").click(function () {
$("#mainmenu li").removeClass("selected").removeAttr("class");
});
});
</script>
</head>
<body>
<header>
<div class="content-wrapper">
<p>
<img src="~/Images/logo.png" alt="" />
</p>
<nav>
<ul id="mainmenu" class="maintablist">
<li class="selected">@Html.ActionLink("View OnCall Schedule", "ViewSchedule", "Home")</li>
<li>@Html.ActionLink("Edit OnCall Schedule", "EditSchedule", "Home")</li>
<li>@Html.ActionLink("Admin OnCall Application", "Admin", "Home")</li>
</ul>
</nav>
</div>
</header>
<div id="body">
@RenderSection("featured", required: false)
<section class="content-wrapper main-content">
@RenderBody()
</section>
</div>
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</body>
</html>