4

javascript based tag ( type ='file' ) created

and add one attribute in that tag

that attribute name onchange, i will assign alert

But alert is not come when choice the new file in internet explore.

choicefile.setAttribute("onChange", "alert('test')");
MaxArt
  • 22,200
  • 10
  • 82
  • 81
sabari
  • 502
  • 1
  • 8
  • 18

5 Answers5

3

You can do two ways,

1.. Using HTML, add onchange event inline

<input type="file" id="file_select" name="file_select" value="" onchange="alert('File selected')" />

Demo: http://jsfiddle.net/CS3xJ/1/

2.. Using JS,

  choicefile.onchange = function(){
     alert('File selected')
  }

Demo: http://jsfiddle.net/CS3xJ/2/

Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
1

There is actually a difference between setAttribute and attachEvent. Here is an example using attachEvent (for IE) and addEventListener (standards) to add the event.

Also, not that the event handler is a function, rather than a string:

var eventHandler = function () {
    alert("Test");
}

if (choicefile.addEventListener) {
  choicefile.addEventListener('change', eventHandler , false);
} else if (choicefile.attachEvent)  {
  choicefile.attachEvent('onchange', eventHandler );
}
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • do bear in mind that `onchange` doesn't bubble in IE on select elements. To delegate an onchange event you'll need [a rather lengthy workaround](http://stackoverflow.com/questions/11331203/javascript-how-to-simulate-change-event-in-internet-explorer-delegation) – Elias Van Ootegem Dec 04 '12 at 11:39
0

Try with this:

choicefile.onchange = function() {alert("test");};
MaxArt
  • 22,200
  • 10
  • 82
  • 81
0

Your code seems correct. Something particular with IE is, if you put higher security level, you need to allow scripts and activeX content when you load the website.

Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57
0

try onclick="javascript:alert('test');" instead of onchange. Old ie versions and compatibility modes don't support onchange very well.