DOM error while using .classList.add method

Viewed 5408

So when I'm trying to add two classes to my elemeny by this code

I have this error

Uncaught DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided ('1 cell-red') contains HTML space characters, which are not valid in tokens.

I don't see any incorrect white space in here

2 Answers

What seems to be the issue is that curr_class is a string containing a space

According to the doc https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList

You should add the class one by one.

Note also that what your code does is : it takes the class of an element and adds it to the same element so nothing usefull

It seems like you have a space in your class name: 1 cell-red between 1 and cell-red.

One solution would be to add the classes one by one:

myElement.classList.add('1');
myElement.classList.add('cell-red');
Related