Last updated: Mar 5, 2024
Reading time·4 min
To remove a class if it exists on an element, select the element and pass the
class name to the classList.remove()
method, e.g.
box.classList.remove('bg-yellow')
.
The remove()
method will ignore any of the supplied classes if they aren't
present on the element.
Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> <style> .bg-yellow { background-color: yellow; } </style> </head> <body> <div id="box" class="bg-yellow">Box 1</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const box = document.getElementById('box'); box.classList.remove('bg-yellow');
We selected the element using the document.getElementById()
method.
We used the classList.remove() method to remove a class if it exists on the element.
remove
method doesn't throw an error, it just ignores the request to remove the specific class.You can test this by duplicating the line that removes the class from the element.
const box = document.getElementById('box'); box.classList.remove('bg-yellow'); // 👇️ has no effect because the class was already removed box.classList.remove('bg-yellow');
You can use the remove()
method to remove as many classes as necessary in a
single call.
If any of the classes are not present on the element, they will be ignored.
const box = document.getElementById('box'); box.classList.remove( 'bg-yellow', 'second-class', 'third-class' );
Similarly, if you need to add a class to an element if it isn't already present,
you can use the classList.add()
method.
const box = document.getElementById('box'); box.classList.remove( 'bg-yellow', 'second-class', 'third-class' ); box.classList.add( 'first-class', 'second-class' );
add()
method only adds classes that are not already contained in the element's class list.Note that the classList.remove()
method has to be called on a DOM element.
If you have a collection of elements you need to remove a class from, you have to iterate over the collection and call the method on each node.
Here is the HTML for the next example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> .bg-yellow { background-color: yellow; } </style> </head> <body> <div class="box bg-yellow">Box 1</div> <div class="box bg-yellow">Box 2</div> <div class="box bg-yellow">Box 3</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const boxes = document.querySelectorAll('.box'); for (const box of boxes) { box.classList.remove( 'bg-yellow', 'second-class', 'third-class' ); }
We used the document.querySelectorAll()
method to select all of the DOM
elements that have a class of box
.
To iterate over the elements, we use the for...of
loop and remove a class from
each element in the collection.
To add a class if it doesn't already exist on an element, select the element and
pass the class name to the classList.add()
method.
The add()
method will omit any classes that are already present on the
element.
Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> .bg-yellow { background-color: yellow; } </style> </head> <body> <div id="box">Box 1</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const box = document.getElementById('box'); box.classList.add('bg-yellow');
We selected the element using the document.getElementById()
method.
We used the classList.add() method to add a class if it doesn't already exist to the element.
add()
method only adds classes that are not already contained in the element's class list.You can test this by duplicating the line that adds the class and inspecting the element.
const box = document.getElementById('box'); box.classList.add('bg-yellow'); // 👇️ has no effect because the class already exists box.classList.add('bg-yellow');
You can use the add()
method to add as many classes as necessary to the
element and only the classes that are not already present on the Node will get
added.
const box = document.getElementById('box'); box.classList.add( 'bg-yellow', 'second-class', 'third-class' );
Similarly, if you need to remove a class only if it exists on the element, you
can use the classList.remove()
method.
const box = document.getElementById('box'); box.classList.add( 'bg-yellow', 'second-class', 'third-class' ); box.classList.remove( 'second-class', 'third-class', 'example' );
If the provided class does not exist on the element, the remove()
method does
not throw an error, instead, it simply ignores the class.
Note that the classList.add()
method has to be called on a DOM element, so if
you have a collection of elements you need to add a class to, you have to
iterate over the collection and call the method on each node.
Here is the HTML for the next example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> .bg-yellow { background-color: yellow; } </style> </head> <body> <div class="box">Box 1</div> <div class="box">Box 2</div> <div class="box">Box 3</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const boxes = document.querySelectorAll('.box'); for (const box of boxes) { box.classList.add('bg-yellow', 'second-class', 'third-class'); box.classList.remove('second-class', 'third-class', 'example'); }
We used the document.querySelectorAll
method to select all of the DOM elements
that have a class of box
.
To iterate over the elements, we use the for...of
loop and added a class to
each element in the collection.