New to Telerik UI for ASP.NET Core? Start a free 30-day trial
Highlighting Selected Menu Item
Environment
Product Version | 2023.2.829 |
Product | Menu for Progress® Telerik® UI for ASP.NET Core |
Description
How can I highlight the selected menu items when using Telerik UI for ASP.NET Core Menu component?
Solution
The example implementation relies on the following key steps:
- Create a
Menu
component with child items. - Within the
$(document).ready()
function, loop through the Menu items elements, and check if the item'shref
attribute equals the entire URL of the current page. If yes, add a classselected-item
to the respective element. - Add the desired background color with CSS to the
selected-item
class and thek-state-highlight
class, which indicates the current active Menu item.
_Layout.cshtml
@(Html.Kendo().Menu()
.Name("Menu") // Add a name to the Menu component.
.Items(items =>
{
items.Add().Text("Contacts").Action("Contact", "Home") // The action method is used to link between pages.
.Items(children => // Add child items to the parent Menu items.
{
children.Add().Text("Contact child 1").Action("Contact1", "Home");
children.Add().Text("Contact child 2").Action("Contact2", "Home");
children.Add().Text("Contact child 3").Action("Contact3", "Home");
});
items.Add().Text("About").Action("About", "Home")
.Items(children =>
{
children.Add().Text("About child 1").Action("About1", "Home");
children.Add().Text("About child 2").Action("About2", "Home");
});
items.Add().Text("Home").Action("Index", "Home");
})
// IMPORTANT: The jQuery and CSS selectors of the Menu component element must match with the Name() of the Menu.
)