{% include "abc.html" %}
In Django, I put my html in this.
Now, my javascript won’t work.
$(function(){
alert('hi');
$(".div_inside_abc").css("background","#000");
});
I even tried putting this javascript inside abc.html (at the top). It still won’t work.
When I do:
$(".div_inside_abc").each(function(e){
document.write($(this).attr("data-rel"));
});
I get this:
{{p.grid_image_ur l}}{{p.grid_image_ur l}}{{p.grid_image_ur l}}{{p.grid_image_ur l}}{{p.grid_image_ur l}}{{p.grid_image_ur l}}
That’s not normal! It’s as if Django’s template is not rendering when I “include” something.
If you put JavaScript code inside HTML it won’t execute (or be valid HTML). You have to wrap it in a SCRIPT tag if you’re going to do it that way (which I don’t advise) or as a DOM1 attribute.
If you want jQuery to run this function on load I’d suggest doing this in an externally referenced JavaScript file that’s loaded after jQuery,
Or adding it inline as,
after the jQuery include.
If you want this to be evaluated arbitrarily I’d suggest invoking the function directly this way (if you need the scope)