• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / Using jQuery to Replace a Class

Using jQuery to Replace a Class

April 18, 2022 Leave a Comment

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:

<div id="div1" class="class-to-be-replaced">
  <p class="p">This is a paragraph.</p>
</div>

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:

<div id="div1" class="new-class">
  <p class="p">This is a paragraph.</p>
</div>

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.


<style>.p-old { background-color: yellow; }.p-new { text-decoration:underline; }</style>
<div id="div1">
  <p class="p-old">This is the paragraph we will want to replace the class of.</p>
  <div id="click-me">Replace Class of the Paragraph Above</div>
</div>

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:


<style>.p-old { background-color: yellow; }.p-new { text-decoration:underline; }</style>
<div id="div1">
  <p class="p-old">This is the paragraph we will want to replace the class of.</p>
  <div id="click-me">Replace Class of the Paragraph Above</div>
</div>

<script>

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

</script>

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:


<style>.class-1 { text-decoration: underline; } .class-2 { background-color: yellow; } .class-3 { font-size: 20px; } .class-4 { font-weight: bold; }</style>
<div id="div2">
  <p class="class-1 class-2">This is the paragraph we will replace multiple classes of.</p>
  <div id="click-me-1" class="click-me">Replace Classes of the Paragraph Above</div>
</div>

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:


<style>.class-1 { text-decoration: underline; } .class-2 { background-color: yellow; } .class-3 { font-size: 20px; } .class-4 { font-weight: bold; }</style>
<div id="div2">
  <p class="class-1 class-2">This is the paragraph we will replace multiple classes of.</p>
  <div id="click-me-1" class="click-me">Replace Classes of the Paragraph Above</div>
</div>

<script>

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

</script>

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Disable Input
  • 2.  jQuery keydown Method
  • 3.  Using jQuery to Get Text Length
  • 4.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript
  • 5.  Using jQuery to Disable a Button
  • 6.  Input Mask Phone Number Using jQuery
  • 7.  How to Clear an Input Field Using jQuery
  • 8.  jQuery last() – Get the Last Element
  • 9.  Using jQuery to Hide Element by Class
  • 10.  Using jQuery to Get the Bottom Position of Element

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy