-1

How can i recreate the following code in pure JavaScript?

$('#img').on('load error', function() { something(); });
Pavlo Zhukov
  • 3,007
  • 3
  • 26
  • 43
Cur
  • 3
  • 1
  • What you are looking for is Custom Events in Javascript. This website explains that in detail: [Creating and triggering events](https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events) – icecub Apr 15 '17 at 18:53
  • What research have you done? What have you tried? Showing your attempts and efforts is important here, this isn't a free code conversion site. If you have code that isn't working as expected however, post that and you will get lots of help. See [ask] – charlietfl Apr 15 '17 at 18:55
  • Possible duplicate of [Binding multiple events to a listener (without JQuery)?](http://stackoverflow.com/questions/8796988/binding-multiple-events-to-a-listener-without-jquery) – Pavlo Zhukov Apr 15 '17 at 19:17

1 Answers1

0

Try this:

<img id="image" src="image.gif" onerror="myFunction()">

Or inline:

document.getElementById("image").addEventListener("error", function(){ something(); });
document.getElementById("image").src = filename;

Or from scratch:

var img=new Image();
img.onload=function(e) { somethingGood(); };
img.onerror=function(e) { somethingBad(); };
img.src=URL;
fmacdee
  • 2,353
  • 10
  • 15