简体   繁体   中英

How to assign backgroundColor to a var in JavaScript

seems like a simple question but I don't know JavaScript too well and my search-fu is turning up nothing relevant.

I need to set the backgroundColor property of a textbox to a value set as a String in a var, like so:

var pinColor = someFunctionThatReturnsString();
document.getElementById("pin").style.backgroundColor = pinColor;

If I put a literal there instead of a var, like '#ffff00', then it works fine. However a var with the String value '#ffff00' does not work (causes no error but does not set the background either). I have to use a var for the assignment because the information is coming from somewhere else, but I can't figure out how to make the backgroundColor respect a String value. Is there some conversion I have to do?

Thank you!

Is there some conversion I have to do?

No, there isn't. It doesn't matter if you use a string literal or a variable.

You have misdiagnosed the problem.

See a live example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM