Reputation: 96810
var css = ["font-family"];
var a = document.getElementById("t");
for (var i = 0; i < a.length; i++) {
a.style[css] = ["Arial"];
}
<div id="t">Hello World</div>
It doesn't add any font to it.
Upvotes: 0
Views: 69
Reputation: 816422
Because the for
loop is never run. a.length
will give undefined
as a
is not an array but a DOM element. i < undefined
will always evaluate to false.
And as @Box9 mentioned, don't put "font-family"
and "Arial"
in arrays. Pass the strings directly:
var css = "font-family";
var a = document.getElementById("t");
a.style[css] = "Arial";
or
document.getElementById("t").style[css] = "Arial";
As you can also see in the function name, Element
is singular, not plural.
Upvotes: 3
Reputation: 16018
In addition to Felix's answer, your style assignment doesn't make sense. You want something like:
a.style.fontFamily = "Arial";
Upvotes: 1