0

I can't seem to make onchange fire for an input type of file. If I directly call an alert (like onchange="alert('a')"), it fires but if I call a user-defined function with only an alert inside, it doesn't. see below:

the html:

<input type='file' value='C:\fakepath' onchange="previewFile()"/>

the script:

function previewFile() {
    alert("a");
}

http://jsfiddle.net/vDQxj/276/

g_b
  • 11,728
  • 9
  • 43
  • 80
  • :/ http://stackoverflow.com/questions/20950417/why-is-a-named-function-inside-an-immediately-invoked-function-expression-unde – loveNoHate Sep 18 '14 at 03:26

2 Answers2

1

The reason that's not working is kind of an artifact of using JSFiddle. In the left panel, you specified to execute the JavaScript onLoad. You need to use the option No wrap - in < head > which is normally where you would place JS in a real HTML file. Change that option / check out this version of your fiddle with the option changed.

http://jsfiddle.net/pxsjjgx5/

p e p
  • 6,593
  • 2
  • 23
  • 32
  • Ok, I tried it using No wrap - in and it works in jsfiddle. This would mean that the js is in the body of the html, right? Funny though, I asked this because my original script was not working even though it is in a script tag inside a ContentPlaceHolder in the bottom part of the body... – g_b Sep 18 '14 at 04:33
  • Ok, I've removed runat="server" in the input type file and now it is suddenly working. Any ideas on why might that be? – g_b Sep 18 '14 at 04:58
  • It looks like what's happening is the id of the input element is changed with runat=server. Any ideas on what can I do with this aside from removing runat=server? – g_b Sep 18 '14 at 05:07
1

Here is the answer for your question. Use jQuery for this and write your code in place of alert in my code. $("input[type=file]").bind("change", function() { alert(); });

SPViradiya
  • 117
  • 7
  • Here is the http://jsfiddle.net/SPViradiya/qx7zq/1/ . If you do not want on click, remove the click word or use the above code. – SPViradiya Sep 18 '14 at 03:41