CamilleFF
CamilleFF

Reputation: 90

Change output result color dynamically

I try to change the color of a javascript result depending it's positive or negative.
I made many tries but not found the solution. Below my last try:

Javascript:

var calc = (a + b) * c;
document.getElementById('result').innerHTML = calc;
(0 < calc) ? document.getElementById('result').style.Color='green':document.getElementById('result').style.Color='red';

Html:

Result: <output id="result"></output>

Any idea ? Thanks

Upvotes: -1

Views: 54

Answers (1)

djcaesar9114
djcaesar9114

Reputation: 2137

This is better:

var [a,b,c] = [1,2,3]
var calc = (a + b) * c;

document.getElementById('result').innerHTML = calc;
document.getElementById('result').style.color= (0 < calc) ? 'green':'red'
Result: <output id="result"></output>

Upvotes: 3

Related Questions