I am attempting to run the following jquery example in my django based website.I am still in the learning phase so please bear with me.
This is what portion of my code looks like so far
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<input type='file' />
<img id="myImg" src="#" alt="your ...image" />
<script>
$(function () {
$(":file").change(function () {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
});
function imageIsLoaded(e) {
alert("dsdsdsd")
$('#myImg').attr('src', e.target.result);
};
</script>
{% endblock %}
It seems like when I select a file the $(":file").change function is not called. Any ideas why that might not be called. I tried this on jsfiddle and it works. I am on chrome. This is what the chrome debug output shows
On hoovering over the x it states
Uncaught Reference Error $ is not defined.
My jquery is loaded. Thats what chrome says

Update:
so it works like this
<script>
function startup()
{
$(function () {
$(":file").change(function () {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
});
function imageIsLoaded(e) {
$('#myImg').attr('src', e.target.result);
};
}
</script>
<body onload="startup()">
I am not sure why it works like this. Any explanation would be appreciated.
Why does it need to be inside the startup

jQuerymost likely isn't loaded when the script is being read/executed. Make sure yourjQuerylibrary is read in first.<script>tag and that is at the end<script src='jQuery.js'>...in the<head>tags