Get elements by class name with Elm.Browser.Dom

Viewed 457

I know I can get an Element by id with Browser.Dom.getElement.

But how can I get a List Element by classname?

1 Answers

As of Elm 0.19, the browser package doesn't expose any other helper functions to query the DOM. The getElement function itself is directly calling a kernel function:

getElement : String -> Task Error Element
getElement =
    Elm.Kernel.Browser.getElement

Depending on what you want to do specifically, you might want to write a JavaScript function that queries the elements, reads the interesting bits, and makes the result accessible to your Elm application through the ports system.

For instance, take a look at the elm-dom-ports package for inspiration. It exposes document.querySelectorAll() function as a port and you can catch its result by subscribing to querySelectorAllResponse.

Related