MooMoo
MooMoo

Reputation: 1116

How to extract value from Html.Attribute in Elm

I have a problem when I want to read Html.Attribute in Elm language. I want to make a custom tag like below;

myTag : List ( Html.Attribute msg ) -> List ( Html msg ) -> Html msg
myTag attributes children = 
    -- case attributes has "src" show button with image
    -- otherwise show normal button

so when I call myTag, if I specific src, I will get button with image automatically, such as;

myTag [ src="somepic.jpg" ][]  -- show a button with image

Or

myTag [][]                     -- show a normal button

I tried to debug to see what is inside "Html.Attribute". It contains something like ( a2 "<tag-name>" <tag-value> ) or ( a3 "<tag-name>" <tag-value> ). However, I do not know how to extract "<tag-name>", which I think it is a String from those values

Upvotes: 1

Views: 512

Answers (1)

Chad Gilbert
Chad Gilbert

Reputation: 36375

The Html.Attribute type is opaque. There is no information you can pull out of it for use inside an Elm application, because the package offers no way to extract data or inspect an Attribute value from within Elm code.

Yes, you can use a javascript debugger to look at how Elm code compiles down to Javascript, but this won't help much in deconstructing the Attribute type from within Elm code.

If you want to inspect attributes, I'd recommend using a Dict String String as your attributes parameter instead:

myTag : Dict String String -> List ( Html msg ) -> Html msg

Upvotes: 3

Related Questions