イベントが発生したときにID属性を取得する(サブスクリプション)

Aug 25 2020

Calculate Stringユーザーがいくつかのdiv(contenteditable)に書き込んでいるときに、何かを計算(msg)するサブスクリプションがあります。

subscriptions : CvModel -> Sub Msg
subscriptions _ = 
    onKeyUp (Decode.succeed Calculate) -- I need add the id element in message.

しかし、idユーザーが書いているdivの属性を取得するにはどうすればよいですか?

回答

4 bdukes Aug 25 2020 at 00:18

渡すデコーダーonKeyUpはイベントオブジェクトをデコードしているためidtarget要素のプロパティを抽出できます(のようにtargetValue)。

onKeyUp (Decode.at ["target", "id"] Decode.string |> Decode.map Calculate)

にサンプルアプリがあります https://ellie-app.com/9MVn2Zsxptva1