The Wayback Machine - https://web.archive.org/web/20240717024446/https://www.geeksforgeeks.org/how-to-add-a-class-to-dom-element-in-javascript/
Open In App

How to Add a Class to DOM Element in JavaScript?

Last Updated : 26 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Adding a class to a DOM (Document Object Model) element in JavaScript is a fundamental task that enables developers to dynamically manipulate the appearance and behavior of web pages. Classes in HTML provide a powerful way to apply CSS styles or JavaScript functionality to multiple elements at once.

By using JavaScript, you can easily add, remove, or toggle classes on elements, making your web applications more interactive and responsive to user actions.

These are the following approaches:

Using classList Property

In this approach, we are using classList property to add the class into the DOM element. It returns the class name as a DOMTokenList object. It has a method called “add” which is used to add class name to elements. we will access the div using the getElementById and we will use the add property of classList to add a class.

Syntax:

element.classList.add("className")

Example: This example shows the implementation of the above-explained approach.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        .geek {
            background-color: green;
            font-size: 50px;
        }
    </style>
</head>

<body>

    <button onclick="myClass()">Try it</button>

    <div id="gfg">Geeks for Geeks</div>

    <script>
        function myClass() {
            let elem = document.getElementById("gfg");

            // Adding class to div element 
            elem.classList.add("geek");
        } 
    </script>

</body>

</html>

Output:

addClass

Using className Property

In this approach, we are using the clasName property. This property returns the className of the element. If the element has already a class then it will simply add another one to it or else it will append our new class to it.

Syntax:

HTMLElementObject.className

Example: This example shows the implementation of the above-explained approach.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        .geekClass {
            background-color: green;
            text-align: center;
            font-size: 50px;
        }
    </style>
</head>

<body>

    <div id="gfg">
        Geeks For Geeks
    </div>

    <button onclick="myClassName()">Add Class</button>

    <script>
        function myClassName() {
            let element = document.getElementById("gfg");

            // Adding the class geekClass to element 
            // with id gfg space is given in className 
            // (" geekClass") as if there is already 
            // a class attached to an element than our 
            // new class won't overwrite and will append 
            // one more class to the element 
            element.className += " geekClass";
        } 
    </script>

</body>

</html>

Output:

addClass

Output



Similar Reads

Replace a DOM element with another DOM element in place
The task is to replace an element by another element in place using JavaScript. Below are the few methods: parentNode Property: This property returns the parent node of the defined node, as a Node object. Syntax: node.parentNode Return value: It returns a node object, which represents the parent node of a node, or null if there is no parent. replac
3 min read
How to Add a new Element to HTML DOM in JavaScript ?
Adding a new element to the HTML DOM dynamically using JavaScript is a common requirement in web development. This allows developers to create interactive and dynamic web pages without the need to reload the entire page. There are several approaches to adding a new element to HTML DOM in JavaScript which are as follows: Table of Content Using creat
2 min read
How to add a new class to an element that already has a class using jQuery ?
In this article, we will learn to add a new class to an element that already has a class using jQuery. There are different methods of achieving this task using the built-in methods available in jQuery. The approaches we can use to add a class to an element using jQuery are listed below: Table of Content Using the addClass() methodUsing the attr() m
3 min read
How to add class to an element without addClass() method in jQuery ?
The task is to add a new class to the element without using addClass() method with the help of jQuery. There are two approaches that are discussed below: Approach 1: To perform the operation, we can use toggleClass() method to toggle the class which we want to add to the element. Example: &lt;!DOCTYPE HTML&gt; &lt;html&gt; &lt;head&gt; &lt;title
2 min read
How to add a class to the last paragraph element using jQuery ?
In this article, we will learn to add a class to the last paragraph element using jQuery. To add a class to the last paragraph element, we use last() and addClass() methods. The jQuery last() function is an inbuilt function that is used to find the last element of the specified elements. Syntax: $(selector).last() The addClass() method is used to a
1 min read
How to add an element horizontally in Html page using JavaScript?
There can be some cases where we have the requirement to add elements in a horizontal manner. Like, if your elements are linked list nodes and you want to add them horizontally. Now the question arises how we can do this in better way.One approach can be to use “display” property with the value “inline grid” as shown in the following program:When t
3 min read
How to add/update an attribute to an HTML element using JavaScript?
To add/update an attribute to an HTML element using JavaScript, we have multiple approaches. In this article, we are going to learn how to add/update an attribute to an HTML element using JavaScript. Below are the approaches used to add/update an attribute to an HTML element using JavaScript: Table of Content Using setAttribute() MethodUsing HTML D
2 min read
How to add an element to a JSON object using JavaScript ?
In JavaScript to add an element to a JSON object by simply assigning a value to a new key. We can use the approaches mentioned below. Let's first create a JSON object in JavaScript and then implement the possible approaches. C/C++ Code let jsonObject = { key1: 'value1', key2: 'value2' }; Examples to add an element to a JSON object using JavaScript1
3 min read
How to Add an ID to Element in JavaScript ?
In JavaScript, the ID is the unique identifier through which the elements can be identified and manipulated if required. We can add an ID to an element in JavaScript using various approaches that are explored in this article with practical demonstration. Below are the possible approaches: Table of Content Using setAttribute()Using Direct assignment
2 min read
How to get the type of DOM element using JavaScript ?
The task is to get the type of DOM element by having its object reference. Here we are going to use JavaScript to solve the problem. Approach 1: First take the reference of the DOM object to a variable(Here, In this example an array is made of IDs of the element, then select random ID and select that particular element).Use .tagName property to get
2 min read
three90RightbarBannerImg