Languages

JavaScript / HTML - onload event doesn't work with div elements

0 points
Asked by:
Krzysiek
741

How can I add onload to the div element? It doesn't seem to work.

Example:

<!doctype html>
<html>
<body>
  <div id="div-id" onload="console.log('Hello World!');">div text</div>
</body>
</html>
1 answer
0 points
Answered by:
Krzysiek
741

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:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <div id="div-id">div text</div>
  <script>console.log('Hello World!');</script>
</body>
</html>

 

See also

0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join