Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 832 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Manipulate the DOM with the classList API

3:26 JavaScript lesson by

Learn how to add, remove and test for CSS classes using the classList API. It's more powerful than using className and doesn't require any dependencies.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Learn how to add, remove and test for CSS classes using the classList API. It's more powerful than using className and doesn't require any dependencies.

Avatar
Thomas

I guess you can have only one button named "toggle filter" (or even a dynamic button text which changes depending on whether the class is applied or not) and then just use classList.toggle('filtername') without a second attribute, which is essentially how .toggle works. (just saying it because it's not clear from the video)

I've set up a basic page with a few elements, and couple of style sheets. In our JavaScript, let's make a reference to our image by saying let image = document.getElementById("image"). We'll say let apply = document.getElementById("apply").

This gives us a reference to our image, and a reference to our apply filter button that we...Let's call our apply button, and say apply.addEventListener, and listen for a click. Inside of that click event listener, we want to say image.classlist.add. I'm going to add a class of moon. You can see when I click the apply filter button, the moon filter was applied.

Classlist.add also takes multiple arguments. If I wanted to add multiple classes, I could do that just by separating those by commas.

I'll add another class of small here. Now when I click the apply filter button, you'll see the filter was applied, and the image gets a little bit smaller. Let's set up a reference to our reset button by saying let reset = document.getElementById("reset").

Just like our apply button, we can add a click event listener on reset by saying reset.addEventListener. Inside of that, we can call image.classlist.remove. That's the exact same signature as add. We can remove moon, and we can remove small.

If I click the apply filter button now, I'll see that those classes were added. When I click the reset button, those classes are removed. Instead of having two buttons to apply the filter, and then reset it, we can change classlist.add to classlist.toggle.

In this case, I just want to toggle the move class. Now when I click apply filter, that class is added. When I click it again, it's removed. Toggle also takes a second parameter. If that second parameter evaluates to true, such as 1<2, classlist will try to toggle the class that you pass on each time it gets evaluated.

Since we know 1<2, when we click apply filter, you'll see the moon class is added. If we click it again, the class is not removed, it's trying to be added again. Really, think of toggle as toggle on or toggle off, based on whether or not this second parameter is true or false.

Let's add some code to our HTML that will let us know whether or not the moon filter is applied. We'll be concerned with the element with the ID of check. Let's create a function called updatecheck(). Inside of our function, we can store a reference to our check element by saying document.getElementById("check").

Let's change the text of our check element by saying check.innerHTML, and set that equal to image.classlist.contains the class of moon. If that evaluates to true, we want to set the text to yes, otherwise we want to set it to no.

Now all that's left to do is call our updatecheck() method, and each time one of our buttons is pressed. You'll see when I click the apply moon filter button, the text changes to yes. If I click reset, it changes back to no.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?