Skip to content
Advertisement

Showing color from ternary operator

See hereI am trying to show a color if my ternary operator comes back as true and another one if it comes back as false. The operator is working but one thing is off. The code inside of the double quotes shows instead of the actual color itself. Anyone know how to fix this ?

JavaScript

Condition is true

Condition is false

EDIT!!! – These are the only times my $msg variable shows up

JavaScript

Advertisement

Answer

Your code is doing exactly what you are telling it: outputting a string that says “background-color: … “.

How would the browser know you want to style the text with that color instead of presenting those words to the user?

You need to instead specify for the browser that this is a style you are giving it.

Instead of

JavaScript

try something like:

JavaScript

COMPLETE EXAMPLE

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement