Add Active Class on Navigation using Jquery

Posted By : Gaurav Arora | 15-Apr-2017

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

About Author

Author Image
Gaurav Arora

Gaurav is an experienced UI developer with experience and capabilities to build compelling UI's for Web and Mobile Applications.

Request for Proposal

Name is required

Comment is required

Sending message..