How to remove a Class name from an Element using JavaScript ? Last Updated : 09 Feb, 2024 Comments Improve Suggest changes Like Article Like Report Removing a class name from an HTML element using JavaScript is often used to dynamically modify the styling or behavior of an element based on user interactions, events, or other conditions. The operation can be achieved using classList property, that allows you to add, remove, or toggle classes. Syntaxparagraph.classList.remove('custom-background');paragraph.classList.toggle('custom-background');Table of Content Using the remove() MethodUsing toggle() MethodUsing the remove( ) MethodTo remove a class name from an element with JavaScript. First style the paragraph with background color. The JavaScript function removeBackgroundColor is defined inside the <script> block. This function is triggered when the button is clicked (onclick="removeBackgroundColor()"). The remove() is called on classList to remove a specified class. It retrieves the paragraph element with the ID myParagraph and removes the class custom-background from it. Example: Illustration of removing class from paragraph element. HTML <!DOCTYPE html> <html lang="en"> <head> <title>Using remove() Method</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <style> body { display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; } .card { width: 300px; padding: 20px; text-align: center; border: 3px solid #755757; border-radius: 20px; } h2 { color: #3cb369; } .custom-background { background-color: #d7f1a7; padding: 10px; } button { margin-top: 10px; padding: 8px; cursor: pointer; border-radius: 10px; } </style> </head> <body> <div class="card" id="myCard"> <h2>GeeksforGeeks</h2> <p id="myParagraph" class="custom-background"> The background color will be removed </p> <div>Click the button to remove the "custom-background" class from the <p> element </div> <button onclick="removeBackgroundColor()"> Click to Remove Color </button> </div> <script> function removeBackgroundColor() { const paragraph = document. getElementById('myParagraph'); paragraph.classList .remove('custom-background'); } </script> </body> </html> Output: OutputUsing toggle() MethodTo remove a class name from an element with JavaScript. First style the paragraph with background color. The JavaScript function removeBackgroundColor is defined inside the <script> block. This function is triggered when the button is clicked (onclick="removeBackgroundColor()"). The toggle() is called on classList to remove and add a specified class. It retrieves the paragraph element with the ID myParagraph and removes the class if present and add if not present. Example: Illustration of removing a class name from an element with JavaScript using toggle method. HTML <!DOCTYPE html> <html lang="en"> <head> <title>Using toggle() Method</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <style> @import url( 'https://fonts.googleapis.com/css2?family=Poppins&display=swap'); body { display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; font-family: 'Poppins', sans-serif; } .card { width: 300px; padding: 20px; text-align: center; border: 3px solid #755757; border-radius: 20px; background-color: antiquewhite; } h2 { color: #0e833b; } .custom-background { background-color: #688338; color: aliceblue; padding: 10px; } button { margin-top: 10px; padding: 8px; cursor: pointer; border-radius: 10px; } </style> </head> <body> <div class="card" id="myCard"> <h2>GeeksforGeeks</h2> <p id="myParagraph" class="custom-background"> The background color will be removed </p> <div>Click the button to remove the "custom-background" class from the <p> element </div> <button onclick="removeBackgroundColor()"> Click </button> </div> <script> function removeBackgroundColor() { const paragraph = document. getElementById('myParagraph'); paragraph.classList .toggle('custom-background'); } </script> </body> </html> Output: Output Comment More infoAdvertise with us Next Article How to remove a Class name from an Element using JavaScript ? S shivanigupta18rk Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Filter a DIV Element Based on its Class Name using JavaScript? Div Element can be filtered based on class name for displaying specific content using JavaScript. Here, we will explore two different approaches to filtering a DIV element.Table of Content Using querySelectorAll and classListUsing getElementsByClassNameUsing querySelectorAll and classListIn this app 3 min read How to remove an HTML element using JavaScript ? Removing an HTML element using JavaScript involves deleting it from the DOM, typically using methods like element.remove() or parentNode.removeChild(). This approach updates the webpage dynamically, allowing for responsive interactions by removing unwanted or outdated elements based on user actions 3 min read How to change style/class of an element using JavaScript ? In this article, we will learn how we can change the style/class of an element. If you want to build a cool website or app then UI plays an important role. We can change, add or remove any CSS property from an HTML element on the occurrence of any event with the help of JavaScript. There are two com 4 min read JavaScript adding a class name to the element In JavaScript, adding a class name to an element allows you to style or manipulate it via CSS or JavaScript. This can be achieved using different methods. Here, we'll explore two common approaches: using the .className property and the .add() method.Below are the different approaches that could be u 3 min read How to Remove HTML element from DOM using AngularJS ? In this article, we will see how to remove particular HTML elements from the DOM with the help of AngularJS, along with understanding its implementation through the examples. For this, we can use the remove() method that helps to remove all the selected elements including all the text. Syntax: selec 2 min read How to Add an Active Class to the Current Element Using JavaScript? We can make use of JavaScript to add an active class to the current element by directly manipulating its class list. This involves identifying the element that triggered the interaction, removing the active class from any previously active elements, and then applying the active class to the current 3 min read How to Hide an HTML Element by Class using JavaScript? To hide an HTML element by class using JavaScript, the CSS display property can be manipulated.Below are the approaches to hide an HTML element by class:Table of ContentUsing getElementsByClassName() selectorUsing querySelectorAll() selectorApproach 1: Using getElementsByClassName() selectorIn this 3 min read How to remove âdisabledâ attribute from HTML input element using JavaScript ? In HTML, we use input elements for user input. The 'disabled' property helps prevent interaction with these elements. For example, if we only want users over 18 to input their Adhar card numbers, we can use JavaScript to remove the 'disabled' attribute when the user enters an age greater than 18. Th 2 min read How to Get Value by Class Name using JavaScript ? This article will show you how to use JavaScript to get value by class name. To get the value of an element by its class name in JavaScript, you can use the getElementsByClassName() method. This method returns an array-like object of all elements with the specified class name. You can then access th 2 min read How To Get Element By Class Name In JavaScript ? When working with the DOM in JavaScript, selecting elements by their class names is a common task. JavaScript provides several methods to achieve this, whether we need to select one or multiple elements. In this article, we will cover different approaches to get elements by class name in JavaScript. 3 min read Like