How to append class name to body with JavaScript

January 01, 2016

There will be times when we need to append class to body or other element when that particular page gets loaded.

The other day, I encountered this scenario while refactoring a Rails app.

Here is how I did so using plain JavaScript

window.onload = function () {
  document.body.className += 'your-awesome-class';

👋 I'm Ashik Nesin • Principal Software Engineer @Chargebee 👨‍💻• I write about web development & behinds the scenes of startup/side projects that I'm building

© 2022 Nesin Technologies LLP