EN
JavaScript - add onload event to div
3
points
In this article, we would like to show you how to add onload event to div using JavaScript.
Quick solution:
You can't. However, you can add
<script>
tag performing some action right after the<div>
to be executed immediately.
<div>Example text...</div>
<script type="text/javascript">
// do something
</script>
Practical example
In this example, we get the element's offsetHeight
property value right after the element is ready in the document body
.
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<div id="my-div">Example text...</div>
<script type="text/javascript">
var element = document.querySelector('#my-div');
// do something on load e.g.:
console.log(element.offsetHeight);
</script>
</body>
</html>