Function is undefined

Your code is contained inside of an IIFE, which prevents outside things from interacting with it, such as your onclick attribute.

A better solution is to remove the onclick attribute, and attach the onclick event from just before the end of your IIFE.

For example:

Remove the onclick attribute:

<img id="dynamic-image1" src="images/zero.gif">

and at the end of the IIFE, assign flipImage1 to the image’s onclick event:

(function () {
    ...
    document.getElementById('dynamic-image1').onclick = flipImage1;
}());
2 Likes