0

I'm trying to get this example working: Preview an image before it is uploaded

I simplified the code to:

<script type="text/javascript">
    $("#imgInp").change(function () {
        alert('changed');
    });
</script>

</head>

<body>
    <form id="form1" runat="server">
        <input type='file' id="imgInp" />
        <img id="blah" src="#" alt="your image" />
    </form>
</body>

The problem is the change function on imgInp is not getting triggered. I've tried a million things, but can't get it working. What am I missing?

Thanks...

Community
  • 1
  • 1

3 Answers3

0

You need to wrap the code in DOM ready:

$(function(){
  $("#imgInp").change(function () {
    alert('changed');
 });
})
Milind Anantwar
  • 77,788
  • 22
  • 86
  • 114
0

You need to place it inside document.ready function...

<script type="text/javascript">
$(document).ready(function(){
    $("#imgInp").change(function () {
    alert('changed');
});
});
</script>
Illaya
  • 646
  • 6
  • 14
0
<script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).on("change","#imgInp",function(){
        alert('changed');
    });

    </script>
Asif Mahamud
  • 573
  • 2
  • 10