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
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:
Share Improve this question edited May 28, 2018 at 8:22 Learner AKS asked Mar 18, 2018 at 14:15 Learner AKSLearner AKS 1831 gold badge3 silver badges10 bronze badges 0String contains an invalid character
2 Answers
Reset to default 11fa 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 ma 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 >
The white space between two class name is creating the issue.If need to add multiple class separate them by a ma and put each of them inside a quote
document.getElementById("sp1").classList.add("fa", "fa-hand-rock-o")
.fa {
color: red;
}
.fa-hand-rock-o {
font-size: 18px;
}
<li id="sp1"> Test Text </li>