MoreLesson 229Free preview
Change Class
This lesson is part of How To · All levels
Summary
Change class means replacing one class with another class on the element, for example from 'info' to 'warning'. The Method deals with substitutions without touching other classes that may still be needed. The alternative is to remove the old class and add the new class in sequence.
Key points
- ClassList.replace('old', 'new') replacing one class with another class
- Method returns true if the old class is found and replaced
- Other classes on the element remained intact
- Avoid className assignment because it overlaps the entire class list
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.