[javascript] How can I change an element's class with JavaScript?

classList DOM API:

A very convenient manner of adding and removing classes is the classList DOM API. This API allows us to select all classes of a specific DOM element in order to modify the list using javascript. For example:

_x000D_
_x000D_
const el = document.getElementById("main");_x000D_
console.log(el.classList);
_x000D_
<div class="content wrapper animated" id="main"></div>
_x000D_
_x000D_
_x000D_

We can observe in the log that we are getting back an object with not only the classes of the element, but also many auxiliary methods and properties. This object inherits from the interface DOMTokenList, an interface which is used in the DOM to represent a set of space separated tokens (like classes).

Example:

_x000D_
_x000D_
const el = document.getElementById('container');_x000D_
_x000D_
_x000D_
function addClass () {_x000D_
   el.classList.add('newclass');_x000D_
}_x000D_
_x000D_
_x000D_
function replaceClass () {_x000D_
     el.classList.replace('foo', 'newFoo');_x000D_
}_x000D_
_x000D_
_x000D_
function removeClass () {_x000D_
       el.classList.remove('bar');_x000D_
}
_x000D_
button{_x000D_
  margin: 20px;_x000D_
}_x000D_
_x000D_
.foo{_x000D_
  color: red;_x000D_
}_x000D_
_x000D_
.newFoo {_x000D_
  color: blue;_x000D_
}_x000D_
_x000D_
.bar{_x000D_
  background-color:powderblue;_x000D_
}_x000D_
_x000D_
.newclass{_x000D_
  border: 2px solid green;_x000D_
}
_x000D_
<div class="foo bar" id="container">_x000D_
  "Sed ut perspiciatis unde omnis _x000D_
  iste natus error sit voluptatem accusantium doloremque laudantium, _x000D_
  totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et _x000D_
  quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam _x000D_
  voluptatem quia voluptas _x000D_
 </div>_x000D_
  _x000D_
<button onclick="addClass()">AddClass</button>_x000D_
  _x000D_
<button onclick="replaceClass()">ReplaceClass</button>_x000D_
  _x000D_
<button onclick="removeClass()">removeClass</button>_x000D_
  
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to dom

How do you set the document title in React? How to find if element with specific id exists or not Cannot read property 'style' of undefined -- Uncaught Type Error adding text to an existing text element in javascript via DOM Violation Long running JavaScript task took xx ms How to get `DOM Element` in Angular 2? Angular2, what is the correct way to disable an anchor element? React.js: How to append a component on click? Detect click outside React component DOM element to corresponding vue.js component