Reputation: 51
I am trying to read JSON data and print it in green or red color depending on the 'scoreChange' being greater or less then zero .
<script>
$.getJSON('/fetchTicker', function (data) {
$.each(data.result, function () {
$("#marqueecontent").append("<span"
if (this['scoreChange'] >= 0){
"style='color:#66FF13'>" + this['playerName']
+ " + " + this['scoreChange'] + " % " + " ▲ ";
}else(this['scoreChange'] < 0){
"style='color:#FF2020'>" + this['playerName']
+ this['scoreChange'] + " % " + " ▼ ";
}
"❘ </span>"
);
});
});
</script>
Upvotes: 0
Views: 36
Reputation: 189
Or just create the object and set the css styles after:
<script>
var span = $('<span />').html(this['playerName'] + ' + ' + this['scoreChange'] + ' % ▲ ');
if (this['scoreChange'] >= 0) {
span.css('color', '#66FF13');
} else {
span.css('color', '#FF2020');
}
$("#marqueecontent").append(span);
</script>
Upvotes: 0
Reputation: 16436
Instead of using if
inside append. Assign color value to variable before it. Then use it in append
<script>
$.getJSON('/fetchTicker', function (data) {
$.each(data.result, function () {
var colorToChange = "";
if (this['scoreChange'] >= 0){
colorToChange = "#66FF13";
}
else
{
colorToChange = "#FF2020";
}
$("#marqueecontent").append("<span style='color:"+colorToChange+"'>" + this['playerName']
+ " + " + this['scoreChange'] + " % " + " ▲ ❘ </span>"
);
});
});
</script>
Upvotes: 1