or, just as simple, go vanilla with:
But what if you wanted to remove all classes starting with a prefix? Assume we have a DOM-Node like this:
We want to remove all the classes starting with “test-“, how do we achieve that?
I came up with a nice little function that does just that.
It takes the node and the prefix for the classes you want removed. Behold the power of RegEx! The query I constructed basically checks for the prefix followed by any character except whitespaces ([^ ]*) followed by one or more whitespaces ([ ]?).
Here’s how that changes the classList:
There you go! We have a simple yet effective to remove classes by prefix without the need for third-party libraries.