In a django project, I'm trying to make a table with cells turning red when user clicks on it.
here is my template :
<table id="this_week" width=88%>
<tr id = "days">
<th><p>Horaire</p></th>
<th><p>lun.</p></th>
<th><p>Mar.</p></th>
<th><p>Mer.</p></th>
<th><p>Jeu.</p></th>
<th><p>Ven.</p></th>
<th><p>Sam.</p></th>
<th><p>Dim.</p></th>
</tr>
{% with hours="9h 9h30 10h 10h30 11h 11h30 12h 12h30 13h 13h30 14h 14h30 15h 15h30 16h 16h30 17h 17h30 18h 18h30 19h 19h30 20h 20h30 21h" %}
{% for i in hours.split %}
<tr id= "{{ i }}">
<th><p>{{ i }}</p></th>
<th id="{{ i }} lun" class="available" onclick='deliveryBooking()'></th>
<th id="{{ i }} mar" class="available" onclick='deliveryBooking()'></th>
<th id="{{ i }} mer" class="available" onclick='deliveryBooking()'></th>
<th id="{{ i }} jeu" class="available" onclick='deliveryBooking()'></th>
<th id="{{ i }} ven" class="available" onclick='deliveryBooking()'></th>
<th id="{{ i }} sam" class="available" onclick='deliveryBooking()'></th>
<th id="{{ i }} dim" class="available" onclick='deliveryBooking()'></th>
</tr>
{% endfor %}
{% endwith %}
</table>
<script>
function deliveryBooking()
{
if ($(this).hasClass('available')) {
$(this).removeClass('available');
$(this).addClass('unavailable');
console.log($(this));
};
};
</script>
But that script does nothing, even in the console log nothing appears. Then I tried to remove the "onclick" attribute on the cells and imported a js file with this code (I tried the following functions one after the other) with no more results :
$(document).ready(function() {
$("#this_week").children().find('.available').click(function(event){
$(event.target).removeClass("available").addClass("unavailable");
console.log($(event.target));
});
$("#this_week").children().find('.available').click(function(){
$(this).removeClass("available").addClass("unavailable");
console.log($(this));
});
});
I also tried to remove the "children()" part and other little changes but I don't know what to try next. Any ideas about what I'm doing wrong ?