Articles in this section
Category / Section

How to change the color of a selected item in JavaScript Menu?

1 min read

This knowledge base explains how to highlight the selected item in JavaScript Menu. It can be achieved by adding the ‘e-select’ class to the selected menu item using the select event in menu. Whenever the required item is selected in the menu, it will be highlighted by the specified color. In the following code example, we have provided the color to be added in the select event if the item is selected.

 

[App.component.ts]

 

import { Component } from '@angular/core';
import { MenuItemModel, MenuEventArgs } from '@syncfusion/ej2-angular-navigations';
 
@Component({
    selector: 'my-app',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    public menuItems: MenuItemModel[] = [
    {
        text: 'File',
        iconCss: 'em-icons e-file'
    },
    {
        text: 'Edit',
        iconCss: 'em-icons e-edit'
    },
    {
        text: 'View'
    },
    {
        text: 'Tools'
    },
    {
        text: 'Help'
    }  
    ];
   // Triggers when the menu item is selected
   private select(args: MenuEventArgs): void {
     // ‘e- selected’ class was added to the selected menu item      if (!args.element.parentElement.querySelector(‘.e-select’))
         args.element.classList.add('e-select'); 
    } else{
              args.element.parentElement.querySelector(‘.e-select’).classList.remove(‘e-select’);
             args.element.classList.add('e-select'); 
       }
    }
}

 

[App.component.html]

 

<div class="menu-control">
        <ejs-menu [items]="menuItems" (select)="select($event)" ></ejs-menu>
</div>
    
 

 

[App.component.css]

 

.e-color{
      background-color: goldenrod;
   }
.menu-control{
      margin-top: 45px;
      text-align: centre;
    }
 
 

 

Sample Link: https://stackblitz.com/edit/angular-u2pegp-mag8qp?file=app.component.ts

 

 

Conclusion

I hope you enjoyed learning about how to change the color of a selected item in JavaScript Menu.

You can refer to our JavaScript Menu feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Menu example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied