Clearing a classList (with the JavaScript Spread Operator)

Previously I have mostly used toggling, adding oder removing classNames from a classList with JavaScript. As these names suggest, they work with classList.toggle(‘className’), classList.add(‘className’) and classList.remove(‘className’). For a recent p…


This content originally appeared on foobartel.com :: Stream and was authored by foobartel.com :: Stream

Previously I have mostly used toggling, adding oder removing classNames from a classList with JavaScript. As these names suggest, they work with classList.toggle('className'), classList.add('className') and classList.remove('className'). For a recent project, I wanted to empty the complete classList of all its className tokens, which made me wonder what the best way to do so would be.

Just like the ways to Rome, there are multiple ways to remove all of your classNames in a class attribute, which all yield the same result. Let's have a look at the possibilities:

First, it is possible to toggle a className, which removes the className if it exists and otherwise adds ist to the classList. Yet this didn’t solve my problem.

element.classList.toggle('is-hidden');

One simple way to completely remove all tokens is to set the class attribute with setAttribute to an empty string, like so:

element.setAttribute('class', '')

Another option is to iterate over all the classNames in the classList and remove the first className as long as classNames can be found. By looking at the following code, I’m not sure why I would pick this, just for the amount of code required. Yet it works absolutely fine.

var classList = element.classList;
while (classList.length > 0) {
   classList.remove(classList.item(0));
}

What I didn’t know is that with ES6, the spread operator (which consists of three periods) offers another simple way to clear a classList. The spread operator description says: “Spread syntax can be used when all elements from an object or array need to be included in a list of some kind.“ This is exactly what was needed in this case.

For my classList example with the spread operator, the syntax also is a one-liner, as compact as the first example above:

element.classList.remove(...element.classList);

From a quick search on the performance difference between setAttribute and using the , it seems that the latter is slightly slower, but performance is not an issue for what I’m using it for.

I hope this has been helpful and thanks for reading this far. The documentation lists lots of other helpful cases for use of the spread operator, it’s worth checking out.


This content originally appeared on foobartel.com :: Stream and was authored by foobartel.com :: Stream


Print Share Comment Cite Upload Translate Updates
APA

foobartel.com :: Stream | Sciencx (2021-02-27T23:00:00+00:00) Clearing a classList (with the JavaScript Spread Operator). Retrieved from https://www.scien.cx/2021/02/27/clearing-a-classlist-with-the-javascript-spread-operator/

MLA
" » Clearing a classList (with the JavaScript Spread Operator)." foobartel.com :: Stream | Sciencx - Saturday February 27, 2021, https://www.scien.cx/2021/02/27/clearing-a-classlist-with-the-javascript-spread-operator/
HARVARD
foobartel.com :: Stream | Sciencx Saturday February 27, 2021 » Clearing a classList (with the JavaScript Spread Operator)., viewed ,<https://www.scien.cx/2021/02/27/clearing-a-classlist-with-the-javascript-spread-operator/>
VANCOUVER
foobartel.com :: Stream | Sciencx - » Clearing a classList (with the JavaScript Spread Operator). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/27/clearing-a-classlist-with-the-javascript-spread-operator/
CHICAGO
" » Clearing a classList (with the JavaScript Spread Operator)." foobartel.com :: Stream | Sciencx - Accessed . https://www.scien.cx/2021/02/27/clearing-a-classlist-with-the-javascript-spread-operator/
IEEE
" » Clearing a classList (with the JavaScript Spread Operator)." foobartel.com :: Stream | Sciencx [Online]. Available: https://www.scien.cx/2021/02/27/clearing-a-classlist-with-the-javascript-spread-operator/. [Accessed: ]
rf:citation
» Clearing a classList (with the JavaScript Spread Operator) | foobartel.com :: Stream | Sciencx | https://www.scien.cx/2021/02/27/clearing-a-classlist-with-the-javascript-spread-operator/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.