When I retrieve the endDate
field from a Django model using a for loop on an HTML page, I want to verify if all the end dates are earlier than today's date. To achieve this, I am utilizing JavaScript code.
Although my code successfully checks the first retrieved date, it fails to work on subsequent dates.
Below is my HTML code for fetching data:
{% for p in object_list %}
<h4>{{ p.jobName }}</h4>
<p><b style="color: red">Expires On: </b><b>{{ p.endDate }}</b></p>
<a href="{{ p.get_absolute_url }}" target="_blank">View info</a>
<input type="hidden" id="endDate" name="variable" value="{{ p.endDate }}">
<span id="check"></span>
{% endfor %}
JavaScript code for date verification:
<script type="text/javascript">
var endDate = document.getElementById("endDate").value;
var ToDate = new Date();
if(new Date(endDate).getTime()<=ToDate.getTime()){
document.getElementById("check").innerHTML = "Expired";
document.getElementById("check").className = "label danger";
}
else{
document.getElementById("check").innerHTML = "Active";
document.getElementById("check").className = "label success";
}
</script>
Currently, I have 2 {{ p.jobName }}
. The first one should display as Expired, while the second should show as Active. However, my code only works correctly for the first date (Computer Admin).
This is the output I'm currently receiving: https://i.sstatic.net/MptWt.png
Could someone identify what might be causing this issue?
Thank you