This sucks, but this is what you need to do:
$('placeholder').insert(new Element("img", { id:'something', src:myImage, onload:'alert("MOO")' }));
The values ββin the attribute object are simply inserted as strings, so when you execute "onload: function () {...}", it turns into:
<img onload="function() {...}" />
Which actually does not execute the code inside the function, it simply defines a new anonymous function that will not be executed if you do not tell it.
If you want to be a ninja about it, you can do something like this:
var moo = function() { alert("MOO"); }; $('placeholder').insert(new Element("img", { id:'something', src:myImage, onload:'(' + moo + ')()' }));
Or even:
$('placeholder').insert(new Element("img", { id:'something', src:myImage, onload:'(' + function() { alert("MOO"); } + ')()' }));
While crazy, these parameters give you the actual function object that you can work with if you need it.
source share