Open In App
Related Articles

How to Add or Remove class in jQuery ?

Improve Article
Save Article
Like Article

The addClass() or removeClass() methods are used to add the CSS classes when there is a need to add to our webpage when there is some event listener or to create some kind of effect.

In this article, let us see how can we add a CSS class or remove a CSS class in jQuery.


  • Adding a class:
  • Removing a class:

Example: The following example adds a class that makes the background colour black when clicked on ADD CLASS button and also removes that added class when clicked on the REMOVE CLASS button.


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <!-- Including jQuery  -->
    <script src=
        h1 {
            color: #006600;
        button {
            color: white;
            background-color: #006600;
            width: auto;
            height: 30px;
        body {
            text-align: center;
        div {
            margin: 10px;
            height: 150px;
            width: 150px;
            position: relative;
            text-align: center;
            display: flex;
            left: 215px;
        .bg-black {
            background-color: black;
    <h1>Geeks For Geeks</h1>
    <button id="btnadd"> ADD CLASS </button>
    <button id="btnremove"> REMOVE CLASS </button>
    <div id="GFG_IMAGE">
        <!-- Image added using img tag 
            with src attribute -->
        <img src=
            height='150px' width='150px'>
        $(document).ready(function() {
            $('#btnadd').click(function() {
            $('#btnremove').click(function() {


Add class and remove class

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 10 Aug, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials