[Answered ]-Forloop.counter0 in my django template is not working under original for loop

1๐Ÿ‘

I actually fixed it myself. I just used the $('[title]').each(function(){}); to iterate over each span created and toggle the class. pretty easy find.

{% for i in inspections.all %}
    <div class="card - mb-3" style="width: 40 rem;">
        <div class="card-body">
            <h3 class="card-title">{{i.Title}} - <span title="inspection" id="s{{forloop.counter0}}" class="">{{i.Condition}}</span>
            </h3>
            <p>{{i.Desc}}</p>
            <h4><span class="badge badge-primary">{{i.Cost}}</span></h4>
        </div>
    </div>
{% endfor %}
<script type="text/javascript">
    $(document).ready(function(){
        $("[title='inspection']").each(function(){
            if ($(this).html() == 'Poor'){
                $(this).toggleClass("badge badge-danger");
            } else if ($(this).html() == 'Not Satisfactory') {
                $(this).toggleClass("badge badge-warning");
            } else if ($(this).html() == 'Satisfactory'){
                $(this).toggleClass("badge badge-success");
            }else if ($(this).html() == 'Not Inspected'){
                $(this).toggleClass("badge badge-secondary");
            }
        });
    });
</script>

Leave a comment