Every developer must learn to remove classes. Eliminating classes can help you create projects like to-do lists and more. Therefore, you should know how to remove a class in JavaScript, as it is one of the easiest ways. This guide will discuss the top 3 methods for removing classes.
The Top 3 methods are:
- element.classList.remove(“className”) method.
- element.classList.toggle(“className”, force) to conditionally remove a class.
- removeClass is a jQuery method.
We will use JS because of its remarkable versatility and ability to run everywhere, which is why JavaScript is so popular. Furthermore, we will discuss how to remove a class in JavaScript in detail and understand them through examples. Without further ado, let’s get started.
Table of Contents
How to Remove a Class in JavaScript? 3 Best Ways
Do you know what a JavaScript class is before learning the methods? If not, don’t worry; we have you covered. A class can be defined as a template for making objects. They store data and include code to utilize that data. Therefore, we now know about classes in JS. Let’s focus on how to remove a class in JavaScript:
element.classList.remove(“className”) method.
The first method to delete a class in JavaScript is the remove method. It is one of the easiest methods. You have to mention the element from which you want the class removed. After mentioning the aspect, you have to pass the remove function, which will then use the classList function of the chosen element. With the help of the classList, you can select and remove a specific class. The best part of this function is that you can specify multiple classes and delete them simultaneously, making it easier and saving time. The syntax for this method is:
element.classList.remove(“className”)
Example
Let’s understand via an example. For instance, look at the following code:
<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<title> How to delete a class in JavaScript</title>
<style>
.example {
width: 90%;
padding: 20px;
background-color: blue;
color: white;
font-size: 30px;
box-sizing: border-box;
margin:15px 0px 0px 0px;
}
</style>
</head>
<body>
<p>Click the “Delete Class” button to delete the “example” class from the DIV element:</p>
<button onclick=”removeClass()”>Delete Class</button>
<div id=”specialDiv” class=”example”>
This is an example.
</div>
<script>
function removeClass() {
var element = document.getElementById(“specialDiv”);
element.classList.remove(“example”);
}
</script>
</body>
</html>
We selected the “specialDiv” element in the above example and deleted the “example” class. We have also included a button that, when clicked, activates the function and deletes the class.
See Also: How To Enable JavaScript On iPad, IPhone IOS(Apple Safari)
element.classList.toggle(“className”, force) to conditionally remove a class
The second method to remove a class in JavaScript is the classList.toggle method. It is the best method to hide and show classes in JavaScript. This method comes under the classList interface in JS. The syntax for this method is:
element.classList.toggle(className, force);
In the above syntax, you can see a force parameter. The force parameter uses a boolean value. For instance, if you set the parameter as True, the class will be shown, whereas if the value is False, it will not reveal the class. However, we will use this method conditionally, i.e., without the parameter. When we use this method without the parameter, it will just toggle between the classes.
Example
Let’s understand via an example. For instance, look at the following code:
<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<title> How to delete class in JS</title>
<style>
.example {
width: 90%;
padding: 20px;
background-color: green;
color: white;
font-size: 30px;
box-sizing: border-box;
margin: 15px 0px 0px 0px;
}
</style>
</head>
<body>
<p>Click the “Delete Class” button to remove the “example” class from the DIV element:</p>
<button onclick=”removeClass()”>Delete Class</button>
<div id=”specialDiv” class=”example”>
This is an element.
</div>
<script>
function removeClass() {
var element = document.getElementById(“specialDiv”);
element.classList.toggle(“example”);
}
</script>
</body>
</html>
We selected the “specialDiv” element in the above example and toggled the “example” class. We have also included a button that, when clicked, activates the function and toggles the class.
removeClass is a jQuery method.
The last method to delete a class is removeClass, a jQuery function. jQuery is a JS library, so we can also remove classes. This method’s workings are different from those of the above methods. For instance, once you remove the class with the help of this method, it cannot be added again. To add the class again, you must incorporate another function: addClass. The syntax is :
$(elementName).removeClass(className);
Example
Let’s understand via an example. For instance, look at the following code:
<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<title>Delete class in JQuery</title>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js” type=”text/javascript”></script>
<style>
.example {
width: 90%;
padding: 20px;
background-color: red;
color: white;
font-size: 30px;
box-sizing: border-box;
margin:15px 0px 0px 0px;
}
</style>
</head>
<body>
<p>Click the “Delete Class” button to Toggle the “example” class from the DIV element:</p>
<button onclick=”removeClassJQuery()”>Delete Class</button>
<div id=”specialDiv” class=”example”>
This is an example.
</div>
<script>
function removeClassJQuery() {
$(“#specialDiv”).removeClass(“example”);
}
</script>
</body>
</html>
We selected the “specialDiv” element in the above example and deleted the “example” class. We have also included a button that, when clicked, activates the function and deletes the class.
See Also: Format Number With Commas In JavaScript | Beginner Guide
FAQs
How to replace class in JavaScript?
To replace a class in JS, you must use the classList property. You must include the function replace(oldClass, newClass) and mention the old and new classes.
How to check if a class exists in JavaScript?
You must use the contains() method to verify whether the class mentioned exists in the classList element.
How to delete a div in JavaScript?
To remove a div in JS, you must use the parentNode. removeChild(element); function.
How do you remove a child in JS?
You must use the removeChild() function to remove a child in JS.
Conclusion
In conclusion, this guide taught us how to remove a class in JavaScript. The methods mentioned in this guide are straightforward and will not cause you to face any difficulties. The toggle method can be a great way to experiment with classes and help you understand what looks good. You can also use the jQuery toggleClass method. Furthermore, you can learn how to convert JavaScript to Typescript or an array to an object in JavaScript to increase your skills as a developer.