How to add active class in menu using jquery
Add active class to a navigation menu item based on URL using jQuery
HTML Code
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about/">About</a></li>
<li><a href="/clients/">Clients</a></li>
<li><a href="/contact/">Contact Us</a></li>
</ul>
</nav>
jQuery Code
<script>
$(function () {
var url = window.location.pathname;
var activePage = url.substring(url.lastIndexOf('/') + 1);
$('#menu-section li a').each(function () {
var linkPage = this.href.substring(this.href.lastIndexOf('/') + 1);
if (activePage == linkPage) {
$(this).parent().addClass('activemenu');
}
});
})
</script>
Author: Learn Tutorials Point
We Well organized and easy-to-understand Interactive tutorials With lots of examples of how to use Tutorials WordPress, PHP, Bootstrap, Bootstrap 4, HTML, CSS, AJAX, jQuery, Woocommerce, Post, Database, Javascript, Theme, Responsive, Templates.
Trending Posts
Tags
ACF
Ading custom columns
admin
Bootstrap
Cart Price
categories
Change Price
class
Code
comments
Create
Creating custom post Type
CSS
current
custom
Customizing
Custom Post
Custom Post Type
Custom Taxonomy
database
Date
Fatch
form
function
HTML
Image
Install
javascript
jQuery
Menu
PHP
Post
Post Type
Price
product
responsive
Resular price
Single product
Table
taxonomy
Theme
Title
woocommerce
Wordpress
Wordpress Code
Impressive!Thanks for the post..
Completely I share your opinion. In it something is also to me it seems it is excellent idea. Completely with you I will agree.
Improbably. It seems impossible.
Super Site New
Amazing post to find here with this much content. I am surprised to see it really happy.
Great content! Keep up the good work!
Everything is very open with a very clear clarification of the challenges. It was really informative. Your site is extremely helpful. Thank you for sharing!