I think that the concept is right here but it doesn’t seem to work and it’s not showing any errors or warnings in the console.
I’m checking checkboxes based on my Django template context. I know that the context is working.
{% for neighborhood in neighborhoods %}
$('#neighborhood_id').each(function(){
if ($(this).val()=={{neighborhood}}){
$(this).attr("checked","checked");
}
});
{% endfor %}
thanks
You need to put quotes around
{{neighborhood}}(I am assuming that it’s a string). Also,$('#neighborhood_id').eachlooks wrong (not that you aren’t allowed to do it) because you are not supposed to reuse IDs, having aneachthere seems like you have more than one. Lastly, why can’t you just check them in your Django template?