Patrick
Patrick

Reputation: 109

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!

Upvotes: 0

Views: 138

Answers (1)

Quentin
Quentin

Reputation: 944546

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.

Upvotes: 3

Related Questions