I have found the ghcjs
and ghcjs-dom
documentation very limited. Here is this basic HTML document.
h1 { font-family: Helvetica; }
p {font-family: Helvetica; color: blue; }
<h1>
Hello World
</h1>
<p>
This is my test document.
</p>
I have read that ghcjs
merely compiles Haskell to JavaScript. If I want to populate the DOM tree even with this simple document, I need to the Foreign Function Interface (FFI) and possibly ghcjs-dom
.
The irony of calling it the "Foreign Function Interface" is that JavaScript is usually considered "native" to the Browser. So there is a tiny bit of terminology confusion there.
In this very simple example, maybe
Let's try a simple example of manipulating the DOM. I have a simple HTML document and I would like to * change the blue paragraph into a red one or * to switch back and forth once each second (between red and blue)
I don't see how the ghcjs set of tools will achieve harder tasks if it cannot even do these very basic test case and explain it. Here is an issue I have raised on Github, with the conclusion that ghcjs lacks a good on-boarding process.
Here's a short self-contained example that uses reflex-dom to do the red/blue color switching that you described. This is a modified version of the code that epsilonhalbe included in this answer to your earlier question.
Of course, reflex-dom (along with reflex) is a higher level library than ghcjs-dom and it comes with its own set of concepts (Event, Dynamic, Behavior, etc) that you need to get comfortable with.
The example works by creating a Dynamic Map that specifies a style that alternates from red to blue each second and using that Dynamic Map to style an element.
For clarity's sake, this example contains some type declarations that are not strictly required.
This project: https://github.com/dc25/stackOverflow__how-to-change-h1-tags-with-ghcjs-dom contains the above code. Here is a link to a browser based demo: https://dc25.github.io/stackOverflow__how-to-change-h1-tags-with-ghcjs-dom/