I am trying to append a div when a checkbox is selected in dajngo template but i keep receiving Uncaught ReferenceError: $ is not defined
in the console
the template:
JavaScript
x
30
30
1
{% extends 'base_layout.html'%}
2
{%load static%}
3
{% block content %}
4
<div class="inventory-content">
5
<div class='category'>
6
<div>Categories</div>
7
<div class='category-checkbox'>
8
{%for category in categories%}
9
<input type="checkbox" id="{{category.id}}" name="{{category.name}}" value="{{category.id}}">
10
<label for="{{category.name}}"> {{category.name}}</label><br>
11
{%endfor%}
12
</div>
13
</div>
14
<div class='items'></div>
15
16
</div>
17
18
<script>
19
$('.category-checkbox input[type="checkbox"]').click(function (){
20
if ($(this).is(':checked')) {
21
// Add the element to the div with an id identifier
22
$('.items').append('<div id="[{{category.id}}]">123</div>');
23
} else {
24
// Remove the element from the div targeted by the id identifier
25
$('.items #[{{category.id}}]').remove();
26
}
27
});
28
</script>
29
{% endblock %}
30
Advertisement
Answer
jquery in django is aliased to django.jQuery instead of $. If you want to use $ instead, you can do something like this:
JavaScript
1
5
1
(function($) {
2
//do some stuff with $ here
3
4
})(django.jQuery);
5