I have a body element on which I add a few classes. And I want to remove the no-javascript
class from it, after it\'s being read by the browser.
There are no native javascript functions for this, but I always use the following code (borrowed from/inspired by this snipplr
function removeClass(ele,cls) {
var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
ele.className = ele.className.replace(reg,' ');
}
removeClass(document.getElementById("body"), "no-javascript")
The regex does a better job than the replace
functions mentioned in other answers, because it checks for the existence of that exact className and nothing more or less. A class named "piano-javascript" would stay intact with this version.
For modern browsers (including IE10 and up) you could also use:
document.querySelector('body').classList.remove('no-javascript');