I am trying to remove the classes that begin with a certain string.
Here is my code:
function myfunction() {
const body = document.getElementsByTagName('div')[0];
body.classList.remove('page*'); //remove page-parent
}
myfunction();
<div class="page-parent pop">
check out
</div>
We can remove the duplicate characters from a string by using the simple for loop, sorting, hashing, and IndexOf() method.
"Every class starting with 'el' is removed from the element."; Select a particular element. Use .className property to get access to all the classes. Use .split () method to get all classes as a element. Use .filter () function to filter out the classes which are not starting with certain character. Finally, put those classes with the element.
What you need to do is parse all of the class names assigned to each element, then remove the ones that meet the correct criteria. That's what rminde's solution does. The reason it is so complicated is because of your "starts with" criteria. Alabama M.. Re: How do I remove all classes that start with a certain name?
Use .split () method to get all classes as a element. Use .filter () function to filter out the classes which are not starting with certain character. Finally, put those classes with the element. Example 2: This example using the approach discussed above. begin with a certain string JavaScript?
Here are a few of the most used techniques discussed. We are going to use JavaScript. 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).
OK , there are many solution to this
first what causes your problem ?
in your code you just remove the class that should be page*
and that's not a regular expression because you're using the double quotes .
also classList
is a DOMTokenList which is a collection , so we need to iterate over it to test each value in the list to see if it meets specific criteria
how to solve this problem ?
first solution
use startWith string method
how ?
example
const p = document.getElementById('p');
p.classList.forEach(item=>{
if(item.startsWith('page')) {
p.classList.remove(item) ;
}
})
.page-blue {
color: blue;
}
.c {
text-align: center;
font-size: 1.4rem;
}
<p id="p" class=" c page-blue">
hello I am p
</p>
also you can use simple regular expression for that
how ? using special-caret that tests if the input starts with the specified value
example
const p = document.getElementById('p');
p.classList.forEach(item=>{
if(/^page/.test(item)) {
p.classList.remove(item) ;
}
})
.page-blue {
color: blue;
}
.c {
text-align: center;
font-size: 1.4rem;
}
<p id="p" class=" c page-blue">
hello I am p
</p>
This is a simple example by using loop regular expression check and remove item which is matched.
function myfunction() {
var div = document.getElementsByTagName('div')[0];
var matches = [];
div.classList.forEach(function (value) {
//remove page-parent
if (/^page.+/.test(value)) {
matches.push(value);
}
});
matches.forEach(function (value) {
div.classList.remove(value);
});
}
myfunction();
<div class="page-parent pop">
check out
</div>
Here's a very "ES6" way of doing it.
We're converting the classList
into an array with a spread operator then we are filtering the array with regex
. This leaves us with an array of classes we want to remove so we can do a forEach
over them and remove them.
I've used a little CSS to display the div
s classes next to it.
const body = document.querySelector('div');
[...body.classList].filter(c => {
return c.match(/^page.*/)
}).forEach(e => {
body.classList.remove(e)
});
div:after {
content: "("attr(class)")";
}
<div class="page-parent pop">
check out
</div>
I hope you find this helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With