Have you considered using a built-in Hype approach instead of code?
You can have the click run a timeline, and that timeline can have keyframes which change the image. The background image is an animatable property - you can hit record, advance the playhead, then use the ‘choose’ button in the background inspector item to have a new keyframe be inserted.
Otherwise, a few issues would be that if you’re doing this from a HTML Widget, you should know that these are iframes, and cannot be communicated to in the same way that you would a regular element. If you need programmatic control to arbitrary HTML, it is much easier to start with a Rectangle element modify via ‘Edit > Edit Element’s Inner HTML’.