How to add onload event to a div element

monkey_boys picture monkey_boys · Oct 30, 2010 · Viewed 649.5k times · Source

How do you add an onload event to an element?

Can I use:

<div onload="oQuickReply.swap();" ></div>

for this?

Answer

DanMan picture DanMan · Oct 30, 2010

No, you can't. The easiest way to make it work would be to put the function call directly after the element

Example:

...
<div id="somid">Some content</div>
<script type="text/javascript">
   oQuickReply.swap('somid');
</script>
...

or - even better - just in front of </body>:

...
<script type="text/javascript">
   oQuickReply.swap('somid');
</script>
</body>

...so it doesn't block the following content from loading.