Is onerror="$.wiki('')" a thing?

Hello all! (SugarCube 2.30.0)

I have a place which gives fallback picture if a variable is not set to a working image link, and I was wondering if I could make it do more things onerror. Here’s the kind of code I have (which I got from the IFCF) which works:

<img @src="$image" onerror="$(this).attr('src', 'fallback.png')">

I was kind of hoping I could do something like this:

<img @src="$image" onerror="$(this).attr('src', 'fallback.png')" onerror="$.wiki('<<widget>>')">

But onerror="$.wiki('<<widget>>')" doesn’t even work on its own, so I’m not sure whether that’s an option.

Is it?

Thank you!

The problem is that you can’t have two attributes with the same name in a single HTML element like that.

The correct way to write that would be like this:

<img @src="$image" onerror="$(this).attr('src', 'fallback.png'); $.wiki('<<widget>>')">

That uses the semicolon to separate the JavaScript commands to be executed when that event is triggered.

Also, note that, while the widget will be executed, you won’t see any output it tries to print to the screen from that. If that’s what you want, you’ll have to make the widget output anything within a <div>, a <span>, or some similar HTML element, likely using the <<replace>> macro to output the content within the element.

Hope that helps! :smiley:

Works like clockwork! Thanks HiEv!