From the course: JavaScript: Enhancing the DOM
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Working with classes
From the course: JavaScript: Enhancing the DOM
Working with classes
- [Instructor] We can do quite a bit in terms of DOM manipulation already, but we haven't dealt with classes just yet. Classes are crucial for styling and identifying groups of elements. JavaScript provides methods to add, remove, and toggle classes. The word class means blueprint of objects in JavaScript and this is very, very different and not related to the HTML class, and that's why the attribute that we're working with is not called class, but class list, to make the distinguishment and not get JavaScript confused with its own definition of class. With classList.add, we add a new class. With classList.toggle, we can use it to toggle the class, and that means that we remove it when it's there and add it when it's not there. With classList.remove, we can remove the class. Here's an example. In order to access the classList property, of course, we would have to select the element first. In this case, we're selecting the toggle button and we use the toggle.classList.toggle, and then…
Contents
-
-
-
-
-
-
(Locked)
Changing text and HTML content2m 51s
-
(Locked)
Changing values of input elements4m 35s
-
(Locked)
Manipulating attributes and data attributes2m 1s
-
(Locked)
Working with classes2m 21s
-
(Locked)
Inline styles with JavaScript2m 9s
-
(Locked)
Challenge: Shapeshifter50s
-
(Locked)
Solution: Shapeshifter47s
-
(Locked)
-
-
-
-
-
-