How to add a class to an element using JavaScript?17 Mar 2025 | 3 min read The class attribute can be used in CSS to do some tasks for the elements with the corresponding class name. In this article, we are discussing how to add a class to an element using JavaScript. In JavaScript, there are some approaches to add a class to an element. We can use the .className property or the .add() method to add a class name to the particular element. Now, let's discuss the approaches to add a class to an element. Using .className propertyThe .className property sets the class name of an element. This property can be used to return the value of the class attribute of an element. We can use this property to add a class to an HTML element without replacing its existing class. To add multiple classes, we have to separate their name with space such as "class1 class2". If a class is already declared for an element, and we need to add a new class name to the same element then it should be declared by inserting a space before writing the new class name otherwise, it will overwrite the existing class name. It can be written as follows: In the above code, we have inserted a space before newClass. SyntaxThe commonly used syntax of this property to set or to return the class name is given below. To set the class name To return the class name The example of using the .className property is given as follows. Example - Adding the class nameIn this example, we are using the .className property for adding the "para" class to the paragraph element having id "p1". We are applying the CSS to the corresponding paragraph using the class name "para". We have to click the given HTML button "Add Class" to see the effect. Output ![]() After clicking the given button, the output will be - ![]() In the next example, we are getting the name of the class by using the .className property. Example - Getting the class nameIn this example, we are using the .className property to get the class names of the paragraph element having id = "p1". We have to click the given HTML button "Get Class name" to see the effect. Output ![]() After clicking the given button, the output will be - ![]() Using add() methodNow, let's see the second approach of adding the class name using JavaScript. We can use the add() method to add a class name to the particular element. SyntaxExampleIn this example, we are using the add() method to add a class name to the paragraph element having id = "p1". We have to click the given HTML button "Add Class" to see the effect. Output ![]() After clicking the given button, the output will be - ![]() |
Blobs are immutable objects that represent unprocessed data. The file is a Blob derived from data from the file system. Blobs allow us to create file-like objects on the client that we may transfer to APIs, and these APIs expect URLs rather than needing the...
5 min read
How to make a curved active tab in the navigation menu using HTML CSS & JavaScript? In this article, we will learn how to make curved outside in active tab in navigation menu using HTML, CSS & JavaScript. We use the HTML list element to create...
10 min read
JS First Class Function JavaScript is a dynamic scripting language that allows us to use it in the form of an object-oriented style or functional style. In the same way, one such feature of JavaScript is the First class function. In this section, we will discuss the...
4 min read
In this article, we are going to discuss about the formatting of numbers with commas in JavaScript. Sometimes, we require formatting a number with commas in the HTML page in order to make it easy to read. Using JavaScript, we can transform a number into...
4 min read
The human eye takes a long time to parse large numerical literals, particularly if many repeated digits exist. A new JavaScript programming feature allows underscores to be used as separators in numerical literals, which makes them easier to understand. A JavaScript feature called numeric separators enables us...
3 min read
In this article, we will understand the Filterable Gallery Using Filterizr javascript library. Filterable Gallery is a customizable component that is used to display collection of images, videos or other media with filtering, sorting, and searching function. Following are the examples of Filterable Gallery Using Filterizr...
7 min read
JavaScript is one of the most renowned and vastly used programming languages. It is growing faster than any other language, and large companies like Netflix, PayPal, and Walmart build internal applications around JavaScript. It is an amazing opportunity to get a fantastic job by learning...
11 min read
The JavaScript change event is an event type that gets executed when the focus on an element is changed. The change event of JavaScript inherits all the methods and properties of the Event. Here, in this section, we will understand and implement the practical use of...
5 min read
? The Promise in JavaScript may look quite complicated to understand at first sight, but in reality, it is quite simple and is not rocket science. In JavaScript, a promise is just like a promise that you make in real life to show that you are...
4 min read
A parent node in JavaScript may have one or more child elements in JavaScript. Now how to fetch those child elements of a node in JavaScript is the question. In this section, we will discuss and look at how we can get child elements of a...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India