2

No jQuery, just JavaScript. I'm using prototype.js but can't find in documentation how can I do that.

I have the img element, and some event that change attr src, so my code is:

document.getElementById('image').addEventListener('change', myFunction(), false);

but this solution doesn't work with img element!

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83

2 Answers2

8

You can't use change event for img tag.

The change event is fired for <input>, <select>, and <textarea> elements when a change to the element's value is committed by the user.

So try like this whenever a image is changed, it will be loaded, so bind it using load event.

Praveen
  • 55,303
  • 33
  • 133
  • 164
1

I too am trying to move an old-IE solution using img onchange, which used to detect change to image's src attribute just fine.

However, the load event solution here (and similarly in Detect a img src change), while great for detecting change to a new image, unfortunately does not fire if you are setting src='' [EDIT: HTML5 does not allow empty, I have changed to use src='data:null'] (I guess because there is no image to load). This is a genuine case for me (user can pick "no image"), and I still want to change attributes like alt & title. Just a heads-up for anyone else.

Community
  • 1
  • 1
JonBrave
  • 4,045
  • 3
  • 38
  • 115