Skip to content

Instantly share code, notes, and snippets.

@rememberlenny
Created April 25, 2013 22:17
Show Gist options
  • Save rememberlenny/5463691 to your computer and use it in GitHub Desktop.
Save rememberlenny/5463691 to your computer and use it in GitHub Desktop.
document class add
$('body').addClass('hasJS');
// or
document.body.className += ' hasJS';
We can do:
document.body.classList.add('hasJS');
Isn’t that pretty?
What about removing:
$('body').removeClass('hasJS');
// or some crazy ass regular express
Or we can do:
document.body.classList.remove('hasJS');
But more impressive is the native toggle support:
document.body.classList.toggle('hasJS');
// and
document.body.classList.contains('hasJS');
To set multiple classes, you add more arguments:
document.body.classList.add('hasJS', 'ready');
What does suck though, is the weird issues – like don’t use a empty string:
document.body.classList.contains('');
// SyntaxError: DOM Exception 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment