It's been a while since I worked with jQuery; I think I'm making a stupid mistake.
Here is a Django widget:
class FooWidget(Widget):
# ...
class Media:
js = ('js/foowidget.js',)
Here is the .js file:
alert("bar");
$(document).ready(function() {
alert("omfg");
$('.foo-widget').click(function() {
alert("hi");
return false;
});
});
alert("foo");
The only alert() that fires is the first. Do I have a stupid syntax error or something?
Also, if some other included script on the page redefines $(document).ready, do I have to worry about it? I'm guessing that subsequent definitions will override mine, so it's safer for subsequent definitions to do something like:
$(document).ready(function() {
document.ready()
// real stuff
});
jQuery is already included in the page by the time foowidget.js is.
Update: Based on @lazerscience's link, I tried the following instead, but it still fails just as before:
alert("bar");
$(function() {
alert("omfg");
$(".set-widget").click(function() {
alert("hi");
return false;
});
});
alert("foo");
Update 2: Curiously, when I do:
alert($);
I get "undefined". This suggests that jQuery is actually not initialized. This confuses me, because the <head> includes:
<script type="text/javascript" src="/media/js/jquery.min.js"></script>
<script type="text/javascript" src="/media/js/jquery.init.js"></script>
<script type="text/javascript" src="/media/js/actions.min.js"></script>
<script type="text/javascript" src="/static/js/foowidget.js"></script>
Doesn't putting my script after the jQuery scripts ensure that $ will be defin