New to Telerik UI for ASP.NET CoreStart a free 30-day trial

Highlighting Selected Menu Item

Environment

Product Version2023.2.829
ProductMenu 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:

  1. Create a Menu component with child items.
  2. Within the $(document).ready() function, loop through the Menu items elements, and check if the item's href attribute equals the entire URL of the current page. If yes, add a class selected-item to the respective element.
  3. Add the desired background color with CSS to the selected-item class and the k-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.
)

More ASP.NET Core Menu Resources

See Also