I attempted to swap a class and came across this helpful example here: javascript: replace classList that is inside a conditional
Unfortunately, my attempt at modification (shown below) did not work.
The use of classList.replace
should change all instances of the class xxx1yyy to xxxyyy
This means I want to replace classes like my1bla, mu1bla, my1hu, …1… and so on with mybla, mubla, myhu, …
<!DOCTYPE html>
<html>
<style>
.my1bla {
background-color: black;
}
.mybla {
background-color: blue;
}
</style>
<p>Click button to change b style class from DIV. background-color will change from black to blue (hopefully)</p>
<button onclick="myFunction()">Try it</button>
<div id="myDIV" class="my1bla">
I am a DIV element
</div>
<script>
function myFunction() {
var x = document.getElementById("myDIV");
if (x.classList.contains("1")) {
x.classList.replace("1", "");
} else if (x.classList.contains("1")) {
x.classList.replace("1", "");
}
}
</script>