How do I change the color of a variable in Javascript? -
here code:
var hi = "hi" document.write(hi) hi.style.color="#ff0000"; document.write(hi)
why won't change colors? keep getting "cannot read property 'style' of undefined".
var hi
string, not dom element, can't apply style it. think you're trying go like:
var hi = "<span style='color:#ff0000'>hi</span>"; document.write(hi);
another option create element on fly:
var myspan = document.createelement('span'); myspan.innerhtml = "hi"; myspan.style.color = "#ff0000"; document.getelementsbytagname('body')[0].appendchild(myspan);
Comments
Post a Comment