We can use jQuery to replace a class (or multiple classes) of an HTML element by simply using the jQuery addClass() method along with the jQuery removeClass() method.

$("#div1").addClass("new-class");
$("#div1").removeClass("old-class");

Let’s say I have the following HTML:

This is a paragraph.

If we want to replace the class “class-to-be-replaced” of #div1 with the class, “new-class”, we can use the jQuery addClass() and removeClass() methods to do this with the following JavaScript code.

$("#div1").addClass("new-class");
$("#div1").removeClass("class-to-be-replaced");

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("#div1").addClass("new-class");
jQuery("#div1").removeClass("class-to-be-replaced");

Using jQuery to Replace a Class With a Click

We can replace a class of an HTML element using jQuery very easily by combining the addClass() and removeClass() methods with a click event.

Let’s say we have the following HTML code and we want to give the user the ability to replace the class of the paragraph. The current class highlights the background of the paragraph. The new class will underline the paragraph content but not highlight it.



This is the paragraph we will want to replace the class of.

Replace Class of the Paragraph Above

We can utilize both the jQuery click() method and jQuery addClass() and removeClass() methods to replace the class “p-old” of the paragraph with the new class, “p-new”.

Below is the JavaScript code which will allow the user to be able to replace the class of the paragraph:

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

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

Code Output:

This is the paragraph we will want to replace the class of.

Replace Class of the Paragraph Above

Full Code:



This is the paragraph we will want to replace the class of.

Replace Class of the Paragraph Above

Using jQuery to Replace Multiple Classes of an HTML Element

We can use the jQuery addClass() and removeClass() methods to replace multiple classes of an HTML element very easily.

The key to replacing multiple classes of our HTML elements is putting a space in between the classes we want to add and remove in the call to addClass() and removeClass().

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

$("#div1").addClass("class-3 class-4");
$("#div1").removeClass("class-1 class-2");

Let’s say we have the following HTML:



This is the paragraph we will replace multiple classes of.

Replace Classes of the Paragraph Above

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

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

The result will be that the div will have a changed background color and will be bold and have larger text.

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

Code Output:

This is the paragraph we will replace multiple classes of.

Replace Classes of the Paragraph Above

Full Code:



This is the paragraph we will replace multiple classes of.

Replace Classes of the Paragraph Above

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

Categorized in:

jQuery,

Last Update: February 26, 2024