EN
JavaScript / HTML - onload event doesn't work with div elements
1 answers
0 points
How can I add onload to the div
element? It doesn't seem to work.
Example:
xxxxxxxxxx
1
2
<html>
3
<body>
4
<div id="div-id" onload="console.log('Hello World!');">div text</div>
5
</body>
6
</html>
1 answer
0 points
The onload
event can't be used with div elements. It can only be used on the document, body, frames, images, and scripts.
However, you can add <script>
tag performing some action right after the <div>
to be executed immediately, like this:
xxxxxxxxxx
1
2
<html>
3
<body>
4
<div id="div-id">div text</div>
5
<script>console.log('Hello World!');</script>
6
</body>
7
</html>
See also
0 commentsShow commentsAdd comment