Hi, In this blog we are going to show that how to add active class on navigation menu using jquery. We can achieve this easily with a simple jquery’s on click function, with the help of add and remove class. And what we gonna do in this function is to add and remove active class on our nav list item.
So, First of all we will require our HTML structure. Here comes our HTML.
Now, we will define some css for active class. To make our selected nav a bit different from the other one’s. For this we will define some css properties.
.active{
color: green;
text-decoration : underline;
}
This will turn our active nav’s list item to green in color and underline it.
Now we will write our jquery function, to apply this active class on our nav list item.
$(function(){
$("li").click(function(){
$("li").removeClass("active");
$(this).addClass("active");
});
});
Now, every time when any list item will be clicked, active class will be added to that list item and we can easily differentiate our active nav’s list with the help of our .active class’s css property.
In this way we can add active class on our nav items with the help of jquery.
THANKS
More From Oodles
Ready to innovate? Let's get in touch
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Gaurav Arora
Gaurav is an experienced UI developer with experience and capabilities to build compelling UI's for Web and Mobile Applications.