Yes, you can change CSS with JavaScript, but you donโt want to do it often (more on how and why in a later lesson).
For now, youโll want to change CSS with classes because thatโs the best way to do itโlet CSS handle CSS stuff.
Adding a class
To add a class, you use the add method. This method can be found within the classList property that exists for all elements.
Element.classList.add('className')Hereโs how you would add the red class to a paragraph of text:
<p>This is a paragraph of text</p>const p = document.querySelector('p')p.classList.add('red')If you check your paragraph in the DOM, you should see the red class:
Adding multiple classes at once
You can add multiple classes to the same element if you pass in extra arguments, like this:
Element.classList.add('firstClass', 'secondClass', 'thirdClass')Note: Thereโs no need to add multiple classes in practice. One class is good enough, provided youโre smart with your classes (donโt worry, youโll see why and how when we build components together).
Removing a class
To remove a class, you use the remove method, which is present in classList.
Element.classList.remove('className')Hereโs how you would remove the red class from a paragraph of text:
<p class="red">This is a paragraph</p>const p = document.querySelector('p')p.classList.remove('red')If you check the DOM, you should see a paragraph without the red class:
Note: You donโt need to add . before your classes when you use Element.classList. This is a common mistake if you mix up the syntax for querySelector and Element.classList.
Removing multiple classes at once
You can remove multiple classes at one go by passing extra classes as arguments:
Element.classList.remove('firstClass', 'secondClass', 'thirdClass')As before, you wonโt have to remove multiple classes. A single class would do, provided youโre smart with your classes.
Checking if a class exists
If you want to check if a class exists, you can use the contains method:
Element.classList.contains('className')This method is often used together with the if statement to check whether the class exists:
const div = document.querySelector('div')if (div.classList.contains('superpower')) { // Do something if the div has the class '.superpower'}Toggling a class
Toggling classes means this:
- Remove
.redif.redexists - Add
.redon if.reddoes not exist
You can toggle a class manually with if/else statements:
if (element.classList.contains('red')) { element.classList.remove('red')} else { element.classList.add('red')}Youโll toggle class on/off frequently when you build componentsโso much that JavaScript provides you with a toggle method that does exactly the same thing.
element.classList.toggle('red')Exercise
Practice adding, removing, checking for classes and toggling classes with Element.classList. Work through the examples in this HTML:
<div class="add">Add a "red" class to me!</div>
<div class="remove">Remove the class, "remove" from me!</div>
<div class="contains1">Do I have a "blue" class?</div><div class="contains2 blue">Do I have a "blue" class?</div>
<div class="toggle"> Do I have a "red" class? If yes, remove it. If no, add it.</div>Add a red class to me!
const add = document.querySelector('.add')add.classList.add('red')Remove the class, remove from me!
const remove = document.querySelector('.remove')remove.classList.remove('remove')Do I have a blue class?
const div1 = document.querySelector('.contains1')const div2 = document.querySelector('.contains2')
div1.classList.contains('blue') // falsediv2.classList.contains('blue') // trueDo I have a red class? If yes, remove it. If no, add it.
const div = document.querySelector('.toggle')div.classList.toggle('red')Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson