1

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

enter image description here

On hoovering over the x it states

Uncaught Reference Error $ is not defined.

My jquery is loaded. Thats what chrome says enter image description here

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

21
  • 2
    That means your jQuery most likely isn't loaded when the script is being read/executed. Make sure your jQuery library is read in first. Commented Jun 23, 2016 at 18:13
  • 1
    I placed the it inside the <script> tag and that is at the end Commented Jun 23, 2016 at 18:13
  • 1
    jQuery should be before your script... not at the end. Commented Jun 23, 2016 at 18:14
  • 1
    Put your <script src='jQuery.js'>... in the <head> tags Commented Jun 23, 2016 at 18:15
  • 1
    @JamesFranco did you get a 200 response on the jquery request? Commented Jun 23, 2016 at 18:25

1 Answer 1

0

This is how I solved the problem

<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()">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.