2
votes

I think I might be missing a key function in the purescript-dom module to convert from a Node to a specific element type. For instance if I have a Event, I can use DOM.Events.Events.target to get the Node, but it seems like the only way to get a specific element is with unsafeCoerce, e.g.

import DOM.Event.Event (target)
import DOM.HTML.HTMLInputElement (value)

eval (InputChange event next) = do
  -- Get the value of the HTMLInputElement assuming it is one
  v <- H.liftEff $ value (unsafeCoerce $ target event)
  H.liftEff $ log "Input field change"
  H.liftEff $ log v
  pure next

Is there a better way to go from a Node to an element type?

1

1 Answers

4
votes

The idea is to use toForeign and then readHTMLInputElement when you want to upcast a Node / element type.

It's pretty annoying to constantly write things like that though, so purescript-dom-classy aims to take some of the pain out of it. As well as avoiding the toForeign step you'll only have a Maybe to deal with, rather than the Except a read function will return.