How to toggle CSS class on <body> tag using vanillaJS – No JQuery


Is it possible to apply a CSS class to the <body> tag of an HTML document?

I have so far been attempting something as such:


The hope is to apply a filter over the entire DOM as to darken the page at specific times, but keep all functionality of the DOM itself.


Or, shorter: document.body.className = 'classname';

As suggested by Gaby, to avoid replacing of already applied class(es):


Answered By – sinisake

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published