We can use jQuery to add a class (or multiple classes) to an HTML element by simply using the jQuery addClass() method.

$("#div").addClass("new-class");

Let’s say I have the following HTML:


This is a paragraph.

If we want to add the class “new-class” to #div, we can use the jQuery addClass() method to do this with the following JavaScript code.

$("#div").addClass("new-class");

The resulting HTML would be as follows:


This is a paragraph.

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div").addClass("new-class");

Using jQuery to Add Class With a Click

We can add a class to an HTML element using jQuery very easily by combining the addClass() method with a click event.

Let’s say we have the following HTML code and we want to give the user the ability to add another class to the paragraph. The new class will underline the paragraph content.



This is the paragraph we will add a class to.
Add Class to the Paragraph Above

We can utilize both the jQuery click() method and jQuery addClass() method to add a class to the paragraph and make the contents underlined.

Below is the JavaScript code which will allow the user to be able to add a class to the paragraph:

$("#click-me").click(function(){
  $(".p").addClass("p-new");
}); 

The final code and output for this example of how to add a class using jQuery and Javascript is below:

Code Output:

This is the paragraph we will add a class to.

Add Class to the Paragraph Above

Full Code:



This is the paragraph we will add a class to.
Add Class to the Paragraph Above

Using jQuery to Add Multiple Classes to HTML Element

We can use the jQuery addClass() method to add multiple classes to an HTML element very easily.

The key to adding multiple classes to our HTML elements is putting a space in between the classes we want to add in the call to addClass

For example, if we want to add classes “class-1” and “class-2” to a specific div, we can do so with the following JavaScript code:

$("#div").addClass("class-1 class-2");

Let’s say we have the following HTML:



This is the paragraph we will add multiple classes to.
Add Classes to the Paragraph Above

If we want to add the classes “class-1” and “class-2” to this div after a click, we just need to do the following in our JavaScript code:

$("#click-me-1").click(function(){
  $(".p1").addClass("class-1 class-2");
});

The result will be that the div will have a changed background color and will have all of it’s text underlined.

The final code and output for this example of how to add multiple classes using jQuery and JavaScript is below:

Code Output:

This is the paragraph we will add multiple classes to.

Add Classes to the Paragraph Above

Full Code:




This is the paragraph we will add multiple classes to.
Add Classes to the Paragraph Above

Hopefully this article has been useful for you to understand how to use jQuery to add a class to an element.

Categorized in:

jQuery,

Last Update: February 26, 2024