I'm creating a table displayed in HTML with Django. I want to change the number's color to red when the number is negative, and to green when the number is positive. I know I need to use JS for this but I could not make it work. Any help will be greatly appreciated !!
Here is my Django HTML template (linked to my view) :
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'WalletJournal/style.css' %}" />
<div id="container">
<h1>Transaction Journal</h1>
</div>
<div id="container">
{% if latest_transaction_list %}
<table>
<tr>
<th>From</th>
<th>To</th>
<th>Amount</th>
<th>Balance</th>
<th>Date/Time</th>
<th>Comment</th>
</tr>
{% for transaction in latest_transaction_list %}
<tr>
<td style="color:white">{{ transaction.TransactionFrom }}</td>
<td style="color:white">{{ transaction.TransactionTo }}</td>
<td style="font-family:'Arial',serif;font-size:10pt"><div class="TransactionAmount">{{ transaction.TransactionAmount }}</div></td>
<td style="font-family:'Arial',serif;font-size:10pt">{{ transaction.BalanceAfterTransaction }}</td>
<td style="font-size:6pt"><a href="{% url 'WalletJournal:detail' transaction.id %}">{{ transaction.TransactionDateTime }}...</a></td>
<td style="color:white">{{ transaction.TransactionComment }}</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>No transactions are available.</p>
{% endif %}
</div>
<script>
var els = document.getElementsByClassName('TransactionAmount');
for (var i = 0; i < els.length; i++) {
var cell = els[i];
if (cell.textContent < 0) {
cell.classList.remove('green')
} else {
cell.classList.add('green');
}
}
</script>
I know the JS code actually works since I got it from my previous question and I tested it outside of my project. Unfortunately my numbers stay gray and don't change color. Even if I use a number like "1" or "-1" instead of {{ transaction.TransactionAmount }}, it still shows up in gray (I tried removing the gray basecolor from the CSS too but it didn't work).
Here's my CSS :
@font-face {
font-family: Eve;
src: url('eve.ttf');
}
@font-face {
font-family: Retro;
src: url('retro.ttf');
}
body {
background: white url("images/background.gif") no-repeat right bottom;
}
h1 {
font-family: Eve;
color: white;
font-size:35pt;
text-align:center;
}
h2 {
font-family: Eve;
color: white;
font-size:20pt;
text-align:center;
}
a:link {
color:#008000;
text-decoration:none;
}
a:visited {
color:#E09016;
text-decoration:none;
}
table, td {
font-family: Retro;
border-style:solid;
border-color:#3A5779;
border-width:5px 5px 5px 13px;
background:#1B2741;
font-size:10pt;
color:gray;
padding:8px;
}
th {
font-family: Eve;
border-style:solid;
border-color:#3A5779;
border-width:5px 5px 5px 13px;
background:#1B2741;
font-size:14pt;
color:white;
padding:15px;
}
#container {
margin: 0 auto;
width: 1000px;
text-align: center;
}
#TransactionAmount {
color: #FF0000;
}
#TransactionAmount.green {
color: #33FF3C;
}
And in case it can help, here's the model I use in Django:
from django.db import models
# Create your models here.
class Transaction(models.Model):
TransactionAmount = models.FloatField("Amount", max_length=75)
BalanceAfterTransaction = models.FloatField("Balance", max_length=75)
TransactionDateTime = models.DateTimeField("Date & Time")
TransactionComment = models.CharField("Comment", max_length=75)
TransactionFrom = models.CharField("From", max_length=75)
TransactionTo = models.CharField("To", max_length=75)
def __str__(self):
return self.TransactionComment
Keep in mind I have limited programming experience, and thx a lot for helping !
id="container") andTransactionAmountis a class and not an id (#TransactionAmount,#TransactionAmount.green)