- [Instructor] Working with element properties like innerHTML is easy enough because we can write directly to them. But what do we do when we have an element that is read-only, like classList? Well, in the case of these read-only elements, you typically have methods you can use to interact with them directly. So for classLists, we have five methods: add, remove, item, toggle, and contains. Let's return to that example from the previous movie for a moment, and see how these methods work on the property. First, I need an element that has a classList, so I say document.querySelector and I remember that masthead has two classes.
Here you see it, masthead and clear. Now I can use a method on this property to change it. So let's say I want to add an additional class. I go to document.querySelector(".masthead").classList and say add, and add a name from new class, return, and if I hit the up key on my keyboard I can scroll through all the different commands I've already passed and I go back to classList and run it again. Now we have masthead, clear, and new class.
You can also see this over here in elements: masthead, clear, and new class. I can also remove a specific class. So if I go back here I can say document.querySelector(",masthead").classList.remove and I want to take away clear. Go back to elements, and clear has been removed, so now I just have masthead and new class. Item, you've already seen, this is when you go into the list, find out what the index number is and call for that particular item.
So if I want new class, which is now item number two, I call for item number one. Toggle allows you to specify a class you want to turn on and off. To make this work we have to target the element using a different class because we'll remove the class that we're targeting. So if I go document.querySelector and I target new class, I get the same element. Then I say classList.toggle and now I can toggle on and off masthead.
And when I do that, you can see it happening in the browser up here, so I'll hit enter, the class masthead is removed from the header and it starts looking weird. Then I just reapply it and it comes back. And you can see in the console here, the first time I run the method I get the response false, meaning we've removed the class name. The second time, I get the response true, meaning we've added the class name back in. The final method, contains, checks if a specified class value exists in the class attribute of the element.
So I can use it to actively see if I have a class in the element without actually doing anything to it. Let's see if masthead exists. So I'll say document.querySelector and instead of toggle I'll say contains, run it, true. Then if I go and toggle masthead off, and run contains again, I get false. Pretty much all these read-only properties for elements have some sort of method you can use to interact with it.
And that's why this MDM page is so important. There is no way you'll remember every single property for every single element, and there is definitely no way you'll remember every single method for every single property that you can apply to elements. Using this reference page, you'll quickly be able to figure out what you can do with different types of elements when you need that specific functionality.
Author
Updated
4/1/2019Released
5/17/2017Through practical examples and mini-projects, this course helps you build your understanding of JavaScript piece by piece, from core principles like variables, data types, conditionals, and functions through advanced topics including loops, closures, and DOM scripting. Along the way, you will also be introduced to some ES6 and the basics of JavaScript libraries.
- What is JavaScript?
- Working with data
- Using functions and objects
- Working with JavaScript and the DOM
- Changing DOM elements
- Handling events
- Working with loops
- Making images responsive using markup
- Troubleshooting code
- Validating functionality
- Minifying JavaScript
Skill Level Beginner
Duration
Views
Related Courses
-
Introduction
-
Welcome1m 7s
-
-
1. JavaScript: An Introduction
-
What is JavaScript?2m 38s
-
-
2. The Basics
-
Introducing the browser console10m 31s
-
3. Working with data
-
Data types in JavaScript4m 2s
-
Arrays2m 20s
-
4. Functions and Objects
-
Functions in JavaScript3m 28s
-
Build a basic function3m 29s
-
Anonymous functions5m 11s
-
Variable scope3m 17s
-
ES2015: let and const6m 12s
-
Make sense of objects3m 19s
-
Object constructors6m 16s
-
Closures8m 11s
-
-
5. JavaScript and the DOM, Part 1: Changing DOM Elements
-
Access and change elements4m 33s
-
Access and change classes3m 45s
-
Access and change attributes4m 53s
-
Add DOM elements6m 56s
-
6. Project: Create an Analog Clock
-
Use CSS to move clock hands3m 49s
-
7. JavaScript and the DOM, Part 2: Events
-
What are DOM events?1m 31s
-
Some typical DOM events1m 59s
-
Add and use event listeners6m 51s
-
-
8. Project: Typing Speed Tester
-
Rundown of HTML markup2m 58s
-
Build a count-up timer5m 56s
-
Add a reset button5m 3s
-
-
9. Loops
-
Loops3m 37s
-
Looping through arrays4m 7s
-
Break and continue loops7m 9s
-
-
10. Project: Automated Responsive Images Markup
-
Project breakdown1m 55s
-
Rundown of project setup3m 26s
-
-
11. Troubleshooting, Validating, and Minifying JavaScript
-
Troubleshooting JavaScript7m 20s
-
Online script linting5m 57s
-
Automate script linting8m 24s
-
Online script minification2m 50s
-
Automate script minification2m 24s
-
12. Bonus Chapter: Ask the Instructor
-
What are arrow functions?3m 11s
-
What does the % symbol do?3m 47s
-
-
Conclusion
-
Next Steps1m 55s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Access and change classes