46

I need to tag my Elm.Http elements with custom "data-*" attributes, for example:

<tr data-row="1">...</tr>

I have tried the following:

import Html exposing (..)
import Html.Attributes exposing (..)
import Json.Encode as JsEncode

view ... =
  tr [ property "data-row" (JsEncode.string (toString 1)) ]

But this does nothing. Anyone know a way?

I think the problem is that Elm is actually setting JavaScript DOM attributes, so I really want to call element.dataset.row = "1" somehow.

The background is I need to expose some data to jQuery for my event handlers, because the Elm event library is missing a bunch of features I need, such as conditional preventDefault and form serialization. There are other ways to supply data via the DOM, but data-* attributes are by far the most straightforward.

1 Answer 1

65

You can use the attribute function instead.

view ... =
    tr [ attribute "data-row" "1" ]
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.