question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add load listener

See original GitHub issue

Hey guys how can i add eventlistener in stimulus way something like this

var div = document.getElementsByTagName("div")[0];
// does not work (console is not written to when page loads)...
div.addEventListener("load", function(){console.log("div load event")}, false);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
robzolkoscommented, Aug 30, 2020

Have you tried connect function? This gets called when the dom element connects to stimulus.

0reactions
javancommented, Aug 31, 2020

To achieve in this raw javascript, i can achieve it to add load eventListener to the element itself

There is no load event for non-media elements (like <div>s). Are you referring to the window load event?

putting them connect does not fit to my problem here, because it will try to re-render again and again the action for the others too at the same time

Stimulus doesn’t do any rendering of its own so I’m not sure what you’re referring to here.

I was trying to achieve to trigger controller action automatically when element is loaded in html page

Stimulus provides two callbacks for that, initialize() and connect(), documented here: https://stimulusjs.org/reference/lifecycle-callbacks


The best place for questions like this is our community forum: https://discourse.stimulusjs.org. Feel free to continue the discussion there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Window: load event - Web APIs - MDN Web Docs - Mozilla
The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets, scripts, iframes, and images.
Read more >
How to Handle the JavaScript onload Event
The image's load event ... The load event also fires on images. To handle the load event on images, you use the addEventListener()...
Read more >
Why doesn't document.addEventListener('load', function) work ...
The problem is WHEN the event is added and EXECUTED via triggering (the document onload property modification can be verified by examining ...
Read more >
onload Event - W3Schools
The onload event occurs when an object has been loaded. onload is most often used within the <body> element to execute a script...
Read more >
GTMTips: Add A Load Listener To Script Elements - Simo Ahava
Tip 66: Add a load listener to script elements. There are two ways you can go about this. The first one is to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found