Hello everybody,

I am building an application in Elm and I need to call an external JS 
library that interacts directly with the DOM (in my case, highlights a 
<pre><code> section). I built a port to communicate from Elm to Javascript 
and, on the Javascript side, I'd like to call my external library as soon 
as the element appears on the page.
My first try was to use the port command in the init function on my Elm 
application, but this doesn't seem to work, because when the JS callback is 
executed, the element of the DOM I need to interact with is not present yet.
For the moment I solved it on the Javascript side, just by retrying until 
the element is actually present, with something like this:

var highlightWhenReady = function () {
    if (!document.getElementById("part-to-highlight")) {
        window.requestAnimationFrame(highlightWhenReady);
    }
    hljs.highlightBlock(document.getElementById("part-to-highlight"));
};

but it seems really dirty, and certainly not the Elm way to do it.
What is the recommended way to do such things the Elm way? How could I know 
when the DOM element actually got rendered in the page?

Thank you!

marco

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to