How to remove classes from DOM elements

TL; DR: Use the remove() method on element.classList

If you have a DOM element reference, you can useremovemethod:

element.classList.remove('myclass')

you can use itaddmethod:

element.classList.add('myclass')

Implementation details:classListNot an array, but a collection of typesDOMTokenList.

You cannot edit directlyclassListBecause it is a read-only attribute. However, you can use its methods to change the element class.

Download mine for freeJavaScript beginner's manual


More browser tutorials: