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.
xxxxxxxxxx
1
<div>Example text...</div>
2
<script type="text/javascript">
3
// do something
4
</script>
In this example, we get the element's offsetHeight
property value right after the element is ready in the document body
.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<div id="my-div">Example text...</div>
5
<script type="text/javascript">
6
7
var element = document.querySelector('#my-div');
8
9
// do something on load e.g.:
10
console.log(element.offsetHeight);
11
12
</script>
13
</body>
14
</html>