Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight menu button?

I need to highlight menu button when selecting menu item. if i select menu item then need to apply current class to (li).

This is what i have tried.

Here is HTML,

  <ul>
     <li class="current"><a href="#">HOME</a></li>
     <li><a href="#">SERVICE</a></li>
     <li><a href="#">ABOUTUS</a></li>
     <li><a href="#">PROFILE</a></li>
     <li><a href="#">CONTACTUS</a></li>
  </ul>

Here is Jquery,

var opener = {
  init:function()  { 
         this.menuselection();
   },
   menuselection:function(){
    $('ul li a').on('click', function (){
        $('ul li').addClass('current');
    });
   }
}
opener.init();

Here is CSS,

.current a {
  color: #03c9a9;
}
a,
a:active,
a:focus {
  color: #fff;
}
a,
a:hover,
a:active,
a:focus {
  outline: 0;
  border: 0;
  text-decoration: none;
  color: #fff;
}

Demo

like image 725
Prasanga Avatar asked Jun 19 '15 04:06

Prasanga


People also ask

How do I highlight items on the windows menu items?

highlighting on Windows menu items. Such as right-click on 'This computer' or Recycle Bin and move the mouse cursor to an item on the menu. The items on the menu do not highlight as the mouse cursor is positioned over them. The menu items in individual programs highlight but not in Windows menu items. Either on

How to Highlight Menu items with CSS in WordPress?

Step 1 – From the WordPress dashboard navigate to Appearance > Menus. Step 2 – Click on Screen Options and tick the CSS Classes checkbox. Step 3 – Click on the menu item that needs to be highlighted. Step 4 – Add CSS class to the menu item and save the changes in the menu.

What is the menu button on a keyboard?

The menu button, also called menu key or application key, is a button which can be found on some Windows-oriented PC keyboards. The menu button is represented by a menu icon with a cursor hovering above it.

How do I make a menu item specific to the page?

You can set the id of the body of the page to some value that represents the current page. Then for each element in the menu you set a class specific to that menu item. And within your CSS you can set up a rule that will highlight the menu item specifically... That probably didn't make much sense, so here's an example:


2 Answers

Use this, small correction in prev comment

 $('ul li a').on('click', function (){
       $('ul li a').removeClass('current');
        $(this).addClass('current');
    });
like image 159
Ishu Khullar Avatar answered Sep 30 '22 04:09

Ishu Khullar


Use $(this) to get the current element and closest('li') to get the li

var opener = {
  init:function()  { 
         this.menuselection();
   },
   menuselection:function(){
    $('ul li a').on('click', function (){
        $(this).closest('li').addClass('current');
    });
   }
}

Otherwise, $('ul li') will add the class to all li elements

Update

if i select $('ul li') , then apply current class then i need to remove all other current class if applied from other li tags.

Before adding the class to $(this).closest('li'), do

$('ul li').removeClass('current');
like image 31
AmmarCSE Avatar answered Sep 30 '22 05:09

AmmarCSE