How to remove all classes that begin with a certain string in JavaScript?

The task is to remove all the classes of a certain element starting with a specific prefix. Here are a few of the most used techniques discussed. We are going to use JavaScript.Â
Approach 1:
- Select a particular element.
- Use .className property to get access to all the classes.
- Use .replace() method to replace all the specific classes by space(Which means classes are removed from the element).
- In this example, a RegExp is used for replacing.
Example: This example using the approach discussed above.Â
html
<script src= </script> <style> Â Â Â Â #div { Â Â Â Â Â Â Â Â height: 100px; Â Â Â Â } Â Â Â Â Â Â Â Â Â Â .el-color { Â Â Â Â Â Â Â Â color: white; Â Â Â Â } Â Â Â Â Â Â Â Â Â Â .el-background { Â Â Â Â Â Â Â Â background: green; Â Â Â Â } Â Â Â Â Â Â Â Â Â Â .el-border { Â Â Â Â Â Â Â Â border: 3px solid blue; Â Â Â Â } </style> Â Â <h1 style="color:green;"> Â Â Â Â zambiatek </h1> <p id="GFG_UP"></p> <div id="div" class="el-color el-background el-border"> Â Â Â Â Div Element </div> <br> <button onclick="GFG_Fun()"> Â Â Â Â click here </button> <p id="GFG_DOWN"></p> <script> Â Â Â Â var el_up = document.getElementById('GFG_UP'); Â Â Â Â var el_down = document.getElementById('GFG_DOWN'); Â Â Â Â el_up.innerHTML = Â Â Â Â "Click on the button to remove all "+ Â Â Â Â "classes starting with certain character."; Â Â Â Â Â Â Â Â Â Â function GFG_Fun() { Â Â Â Â Â Â Â Â $('#div')[0].className = Â Â Â Â Â Â Â Â $('#div')[0].className.replace(/\bel.*?\b/g, ''); Â Â Â Â Â Â Â Â el_down.innerHTML = Â Â Â Â Â Â Â Â "Every class starting with 'el' is removed from the element."; Â Â Â Â } </script> |
Output:
How to remove all classes that begin with a certain string in JavaScript?
Approach 2
- Select a particular element.
- Use .className property to get access to all the classes.
- Use .split() method to get all classes as an element.
- Use .filter() function to filter out the classes which do not start with a certain characters.
- Finally, put those classes with the element.
Example 2: This example uses the approach discussed above.Â
html
<script src= </script> <style> Â Â Â Â #div { Â Â Â Â Â Â Â Â height: 100px; Â Â Â Â } Â Â Â Â Â Â Â Â Â Â .el-color { Â Â Â Â Â Â Â Â color: white; Â Â Â Â } Â Â Â Â Â Â Â Â Â Â .el-background { Â Â Â Â Â Â Â Â background: green; Â Â Â Â } Â Â Â Â Â Â Â Â Â Â .el-border { Â Â Â Â Â Â Â Â border: 3px solid blue; Â Â Â Â } </style> Â Â <h1 style="color:green;"> Â Â Â Â zambiatek </h1> <p id="GFG_UP"></p> <div id="div" class="el-color el-background el-border"> Â Â Â Â Div Element </div> <br> <button onclick="GFG_Fun()"> Â Â Â Â click here </button> <p id="GFG_DOWN"></p> <script> Â Â Â Â var el_up = document.getElementById('GFG_UP'); Â Â Â Â var el_down = document.getElementById('GFG_DOWN'); Â Â Â Â var el = document.getElementById('div'); Â Â Â Â el_up.innerHTML = "Click on the button to remove "+ Â Â Â Â "all classes starting with certain character."; Â Â Â Â Â Â Â Â Â Â function GFG_Fun() { Â Â Â Â Â Â Â Â var startsWith = "el"; Â Â Â Â Â Â Â Â var classes = el.className.split(" ").filter(function(v) { Â Â Â Â Â Â Â Â Â Â Â Â return v.lastIndexOf(startsWith, 0) !== 0; Â Â Â Â Â Â Â Â }); Â Â Â Â Â Â Â Â el.className = classes.join(" ").trim(); Â Â Â Â Â Â Â Â el_down.innerHTML = Â Â Â Â Â Â Â Â "Every class starting with 'el' is removed from the element."; Â Â Â Â } </script> |
Output:
How to remove all classes that begin with a certain string in JavaScript?
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!



