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
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
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.
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.
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:
Use this, small correction in prev comment
$('ul li a').on('click', function (){
$('ul li a').removeClass('current');
$(this).addClass('current');
});
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With