0

I am trying to attach an event lister to a file input. I was thinking it would work like this. But i have read online and cant seem to get it to work.

    <input type="file" id=image1 >

    var image1 = document.getElementsByID("image1");

    image1.addEventListener('change', alert(1));
Cœur
  • 37,241
  • 25
  • 195
  • 267
Holly Johnson
  • 509
  • 1
  • 13
  • 25

3 Answers3

1

Change this:

var image1 = document.getElementsByID("image1");
image1.addEventListener('change', alert(1));

to this:

var image1 = document.getElementById("image1");
image1.addEventListener('change', function(){ alert(image1); });

Because getElementsByID() is not a valid method call, but getElementById() is.

Also, so that you will be executing a function that invokes the alert(), rather than invoking the alert immediately and assigning the result of the alert as the click event handler, we need to wrap the alert() in a function.

Lastly, alert(1) doesn't show anything because 1 isn't a variable, nor is it a string. You should be writing alert(image1.someProperty). Since image1 is a file type, you could access the files associated with the element and then a specific file and then an aspect of that file.

Here's the whole thing:

var image1 = document.getElementById("image1");

image1.addEventListener('change', function(){ 
  var files = image1.files;
  alert(files[0].name); 
});
<input type="file" id=image1 >
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

Try this:

var input = document.getElementsByTagName('input')[0];

input.onclick = function () {
    this.value = null;
};

input.onchange = function () {
    alert(this.value);
};
<input type='file' value='C:\fakepath' />
gaganshera
  • 2,629
  • 1
  • 14
  • 21
0

<input type="file" id="image1" onchange="alert(1);" />

  • Inline HTML event attributes should not be used. – Scott Marcus May 10 '17 at 19:11
  • Why not? Is it not standard ES/HTML? – Anthony Gingrich May 10 '17 at 19:25
  • It is part of the HTML5 standard, but not part of the DOM Event Standard. Here's a link to a previous answer of mine that explains the many reasons not to ever use them. http://stackoverflow.com/questions/43459890/javascript-function-doesnt-work-when-link-is-clicked/43459991#43459991 – Scott Marcus May 10 '17 at 19:26
  • And, here's a link to the DOM Event Standard: https://www.w3.org/TR/dom/#introduction-to-dom-events – Scott Marcus May 10 '17 at 19:26
  • Ha! I rarely write such low-level code these days, so I'm obviously not up-to-snuff on those specific best practices. I can't think of a single web app I've written in the past 3 years where it would have been appropriate for me to use an inline handler. I guess this question seemed to me to be pitched from an academic point of view, so I provided a strictly academic answer. Thanks for the great info! – Anthony Gingrich May 10 '17 at 19:33
  • Frankly, inline HTML event handlers haven't been appropriate since about 2001. – Scott Marcus May 10 '17 at 19:47
  • **From review queue**: May I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. – RBT May 11 '17 at 01:40