How to add onload event to a div element points out that the onload
attribute only works for <body>
and external sources (like <script>
).
So for the example in that question:
<div onload="oQuickReply.swap();" ></div>
the onload contents will never be called.
How can I trigger some javascript to run when a div is added to the page, via the div element HTML itself?
As we've seen
$('<div onload="myFunction()"></div>').appendTo('body');
Won't work. myFunction
will not be called, because onload
isn't even triggered for divs.
I am creating HTML on the backend using C# Razor/DevExtreme, and it doesn't load the entire webpage at once but I want to run some code after some part of the webpage has loaded. This is why i can't just
$('<div></div>').appendTo('body');
myFunction();