[Answer]-Changing class of element in django template using javascript function

1👍

You wouldn’t necessarily have to use javascript to do this. There are a couple of different options using the template tags.

The simpler approach (and more crude) would be to render the current datetime to the template as eg. current_date then do a logic check in your template.

{% for  event in events %}
    {% if event.date_of_occurrence < current_date %}  
    <div id="pastevent">
    {% else %}
    <div id="event_div">
    {% endif %}
    <li>
       event.name<br>
       event.date_of_occurrence
    </li>
  </div>
{% endfor %}

The other option would be to create a custom template filter that returns whether the event was in the past or not and apply the same logic as above.

Leave a comment