Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript element.classList.add("fa fa-hand-rock-o") Error: "String contains an invalid character"

I'm trying to add a class to <span id="sp1"> using:

document.getElementById("sp1").classList.add("fa fa-hand-rock-o");

But it is showing error:

String contains an invalid character

like image 377
Learner AKS Avatar asked Mar 18 '18 14:03

Learner AKS


1 Answers

fa fa-hand-rock-o can not be a single class because class names can not have space(s).

Here I assume you are trying to add two different classes. When adding multiple classes by using classList.add() specify all the classes as individual comma separated string like:

.add("fa", "fa-hand-rock-o")

Code Example:

document.getElementById("sp1").classList.add("fa","fa-hand-rock-o");

console.log(document.getElementById("sp1"));
<span id="sp1">Test Container</span >
like image 90
Mamun Avatar answered Sep 21 '22 01:09

Mamun