Please enable Javascript for better experience...
addClass(), removeClass() in JQuery
By Rahul Kumar Jha | Jun 10, 2015 | In Articles | Total Views [ 2721 ]
Taged In
(2 Like)

You can add/remove single or multiple class to the element dynamically with the help of addClass(), removeClass() methods in JQuery.


Sometimes it need to add or remove class to a element from code. JQuery addClass() and removeClass() methods can be used to add/remove one or multiple class to the element. In this article I will show how these methods can work for you.


The addClass() method adds one or more class names to the elements. This method does not remove any existing class attributes, but only adds class name to the class attribute.


It will add classA to all div elements available in your page.

$('#mydiv').addClass('classA classB')

Above line will add classA and classB to the div elements.


The removeClass() method can remove one/more or all class names from the elements. Below line of code will remove classA from all div elements present in your HTML.


If want to remove all classes from class attribute then don't pass any class name in removeClass() method.

You can also use these methods one after another as per your need. Please see the example below.


It will first remove classA from div element and then will add classB to the element.

$('#mydiv').removeClass().addClass('classA classB')

It will remove all classes from div element and then will add classA and classB to the element.

Hope it can help you. Please don't forget to like and rate the article. I will come up with such articles.

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer

Public profile: user/profile/99900001

Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments

Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID