MoreLesson 227Free preview
Add Class
This lesson is part of How To · All levels
Summary
Add class is used to add one or more CSS classes to an element via JavaScript. The method maintains an existing class and ignores the class that has been added. This approach is safer than overriding the entire class attribute with a classname.
Key points
- ClassList.add('name') adds a class without removing another class
- Several classes can be added at once as separate arguments
- Adding the same class twice doesn't cause duplication.
- Avoid 'new' because you delete an existing class
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.