MoreLesson 226Free preview
Toggle Class
This lesson is part of How To · All levels
Summary
Toggle class means adding a class if it doesn't exist and deleting it if it already exists. Method classList.toggle does both in one call so the code is more concise. This method returns a boolean value that indicates whether a class is currently active, and can accept a second argument to force a particular status.
Key points
- ClassList.toggle('class names') reversed class status
- The return value is true if the class is added and false if deleted
- A second argument like toggle('aktif', true) forces the class to be added
- Suitable for changing themes, menus, or active status without writing if long
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.