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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -