I would like know the attribute html of an element in Elm











up vote
1
down vote

favorite












I would like know the attribute html of an element in Elm, for example, her coordinates. I were trying with Json.Decode.



I am new in Elm, and this is for learning purpose.



This is a simple code, i am using Elm 0.18:



module Stamps exposing (..)

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)


type alias Model =
{}


type Msg
= NoOp
| Clicking


model : Model
model =
{}


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp ->
( model, Cmd.none )

Clicking ->
let
_ =
Debug.log "msg1" model
in
( model, Cmd.none )


view : Model -> Html Msg
view model =
div
[ Html.Attributes.style
[ ( "backgroundColor", "blue" )
, ( "height", "300px" )
, ( "width", "300px" )
, ( "position", "relative" )
, ( "left", "100px" )
, ( "top", "50px" )
]
, Html.Attributes.class
"parent"
]
[ div
[ Html.Attributes.style
[ ( "background-color", "#3C8D2F" )
, ( "cursor", "move" )
, ( "width", "100px" )
, ( "height", "100px" )
, ( "border-radius", "4px" )
, ( "position", "absolute" )
, ( "color", "white" )
, ( "display", "flex" )
, ( "align-items", "center" )
, ( "justify-content", "center" )
]
, Html.Attributes.class
"children"
, Html.Events.onClick Clicking
]

]


subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none


main : Program Never Model Msg
main =
Html.program
{ init = ( model, Cmd.none )
, update = update
, view = view
, subscriptions = subscriptions
}









share|improve this question




























    up vote
    1
    down vote

    favorite












    I would like know the attribute html of an element in Elm, for example, her coordinates. I were trying with Json.Decode.



    I am new in Elm, and this is for learning purpose.



    This is a simple code, i am using Elm 0.18:



    module Stamps exposing (..)

    import Html exposing (..)
    import Html.Attributes exposing (..)
    import Html.Events exposing (..)


    type alias Model =
    {}


    type Msg
    = NoOp
    | Clicking


    model : Model
    model =
    {}


    update : Msg -> Model -> ( Model, Cmd Msg )
    update msg model =
    case msg of
    NoOp ->
    ( model, Cmd.none )

    Clicking ->
    let
    _ =
    Debug.log "msg1" model
    in
    ( model, Cmd.none )


    view : Model -> Html Msg
    view model =
    div
    [ Html.Attributes.style
    [ ( "backgroundColor", "blue" )
    , ( "height", "300px" )
    , ( "width", "300px" )
    , ( "position", "relative" )
    , ( "left", "100px" )
    , ( "top", "50px" )
    ]
    , Html.Attributes.class
    "parent"
    ]
    [ div
    [ Html.Attributes.style
    [ ( "background-color", "#3C8D2F" )
    , ( "cursor", "move" )
    , ( "width", "100px" )
    , ( "height", "100px" )
    , ( "border-radius", "4px" )
    , ( "position", "absolute" )
    , ( "color", "white" )
    , ( "display", "flex" )
    , ( "align-items", "center" )
    , ( "justify-content", "center" )
    ]
    , Html.Attributes.class
    "children"
    , Html.Events.onClick Clicking
    ]

    ]


    subscriptions : Model -> Sub Msg
    subscriptions model =
    Sub.none


    main : Program Never Model Msg
    main =
    Html.program
    { init = ( model, Cmd.none )
    , update = update
    , view = view
    , subscriptions = subscriptions
    }









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I would like know the attribute html of an element in Elm, for example, her coordinates. I were trying with Json.Decode.



      I am new in Elm, and this is for learning purpose.



      This is a simple code, i am using Elm 0.18:



      module Stamps exposing (..)

      import Html exposing (..)
      import Html.Attributes exposing (..)
      import Html.Events exposing (..)


      type alias Model =
      {}


      type Msg
      = NoOp
      | Clicking


      model : Model
      model =
      {}


      update : Msg -> Model -> ( Model, Cmd Msg )
      update msg model =
      case msg of
      NoOp ->
      ( model, Cmd.none )

      Clicking ->
      let
      _ =
      Debug.log "msg1" model
      in
      ( model, Cmd.none )


      view : Model -> Html Msg
      view model =
      div
      [ Html.Attributes.style
      [ ( "backgroundColor", "blue" )
      , ( "height", "300px" )
      , ( "width", "300px" )
      , ( "position", "relative" )
      , ( "left", "100px" )
      , ( "top", "50px" )
      ]
      , Html.Attributes.class
      "parent"
      ]
      [ div
      [ Html.Attributes.style
      [ ( "background-color", "#3C8D2F" )
      , ( "cursor", "move" )
      , ( "width", "100px" )
      , ( "height", "100px" )
      , ( "border-radius", "4px" )
      , ( "position", "absolute" )
      , ( "color", "white" )
      , ( "display", "flex" )
      , ( "align-items", "center" )
      , ( "justify-content", "center" )
      ]
      , Html.Attributes.class
      "children"
      , Html.Events.onClick Clicking
      ]

      ]


      subscriptions : Model -> Sub Msg
      subscriptions model =
      Sub.none


      main : Program Never Model Msg
      main =
      Html.program
      { init = ( model, Cmd.none )
      , update = update
      , view = view
      , subscriptions = subscriptions
      }









      share|improve this question















      I would like know the attribute html of an element in Elm, for example, her coordinates. I were trying with Json.Decode.



      I am new in Elm, and this is for learning purpose.



      This is a simple code, i am using Elm 0.18:



      module Stamps exposing (..)

      import Html exposing (..)
      import Html.Attributes exposing (..)
      import Html.Events exposing (..)


      type alias Model =
      {}


      type Msg
      = NoOp
      | Clicking


      model : Model
      model =
      {}


      update : Msg -> Model -> ( Model, Cmd Msg )
      update msg model =
      case msg of
      NoOp ->
      ( model, Cmd.none )

      Clicking ->
      let
      _ =
      Debug.log "msg1" model
      in
      ( model, Cmd.none )


      view : Model -> Html Msg
      view model =
      div
      [ Html.Attributes.style
      [ ( "backgroundColor", "blue" )
      , ( "height", "300px" )
      , ( "width", "300px" )
      , ( "position", "relative" )
      , ( "left", "100px" )
      , ( "top", "50px" )
      ]
      , Html.Attributes.class
      "parent"
      ]
      [ div
      [ Html.Attributes.style
      [ ( "background-color", "#3C8D2F" )
      , ( "cursor", "move" )
      , ( "width", "100px" )
      , ( "height", "100px" )
      , ( "border-radius", "4px" )
      , ( "position", "absolute" )
      , ( "color", "white" )
      , ( "display", "flex" )
      , ( "align-items", "center" )
      , ( "justify-content", "center" )
      ]
      , Html.Attributes.class
      "children"
      , Html.Events.onClick Clicking
      ]

      ]


      subscriptions : Model -> Sub Msg
      subscriptions model =
      Sub.none


      main : Program Never Model Msg
      main =
      Html.program
      { init = ( model, Cmd.none )
      , update = update
      , view = view
      , subscriptions = subscriptions
      }






      html styles elm






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 11 '17 at 13:07









      lonelyelk

      455717




      455717










      asked Jan 11 '17 at 2:19









      Luis Angel

      434




      434
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          6
          down vote













          So I'm not really sure which of the offsets you're trying to get specifically, but I think this will put you on track. First you need to adjust your Msg ADT's Clicking to have a tuple for the coordinates.



          type Msg
          = NoOp
          | Clicking Int Int


          Assuming you want to stick with just logging the coords for now, you can use this. DO remember that you can destructure in the pattern match if you need.



          update : Msg -> Model -> ( Model, Cmd Msg )
          update msg model =
          case msg of
          NoOp ->
          ( model, Cmd.none )

          Clicking x y ->
          let
          _ =
          Debug.log "coords" ( x, y )
          in
          ( model, Cmd.none )


          You need a Json.Decode.Decoder to that tuple.



          import Json.Decode as Decode exposing (Decoder)


          coordsDecoder : (Int -> Int) -> Decoder a
          coordsDecoder into =
          Decode.field "target" <|
          Decode.map2 into
          (Decode.field "offsetWidth" Decode.int)
          (Decode.field "offsetHeight" Decode.int)


          And lastly, you need the on to take decoder do something with the Event object returned by a JavaScript click event. This decoder gets the coordinates, then Decode.maps the Clicking msg type.



          Html.Events.on "click" (coordsDecoder Clicking)


          With that Decode.field "target" ... you can start decoding whatever you want from the target element.





          Stylistically, you're importing all of the Html.* functions into scope with exposing (..), but you're still prefixing everything which is pretty redundant. You can just use style instead of Html.Attributes.style. Don't be afraid to use as -> Html.Attributes as Attr.






          share|improve this answer






























            up vote
            0
            down vote













            Excellent answer, in case if i try to get the coordinates in relation to the browser, in case the style left and top are not definity. I am searching some solution and found this






            offsetParent : a -> Decoder a -> Decoder a
            offsetParent x decoder =
            Decode.oneOf
            [ field "offsetParent" <| Decode.null x
            , field "offsetParent" decoder
            ]








            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41581686%2fi-would-like-know-the-attribute-html-of-an-element-in-elm%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              6
              down vote













              So I'm not really sure which of the offsets you're trying to get specifically, but I think this will put you on track. First you need to adjust your Msg ADT's Clicking to have a tuple for the coordinates.



              type Msg
              = NoOp
              | Clicking Int Int


              Assuming you want to stick with just logging the coords for now, you can use this. DO remember that you can destructure in the pattern match if you need.



              update : Msg -> Model -> ( Model, Cmd Msg )
              update msg model =
              case msg of
              NoOp ->
              ( model, Cmd.none )

              Clicking x y ->
              let
              _ =
              Debug.log "coords" ( x, y )
              in
              ( model, Cmd.none )


              You need a Json.Decode.Decoder to that tuple.



              import Json.Decode as Decode exposing (Decoder)


              coordsDecoder : (Int -> Int) -> Decoder a
              coordsDecoder into =
              Decode.field "target" <|
              Decode.map2 into
              (Decode.field "offsetWidth" Decode.int)
              (Decode.field "offsetHeight" Decode.int)


              And lastly, you need the on to take decoder do something with the Event object returned by a JavaScript click event. This decoder gets the coordinates, then Decode.maps the Clicking msg type.



              Html.Events.on "click" (coordsDecoder Clicking)


              With that Decode.field "target" ... you can start decoding whatever you want from the target element.





              Stylistically, you're importing all of the Html.* functions into scope with exposing (..), but you're still prefixing everything which is pretty redundant. You can just use style instead of Html.Attributes.style. Don't be afraid to use as -> Html.Attributes as Attr.






              share|improve this answer



























                up vote
                6
                down vote













                So I'm not really sure which of the offsets you're trying to get specifically, but I think this will put you on track. First you need to adjust your Msg ADT's Clicking to have a tuple for the coordinates.



                type Msg
                = NoOp
                | Clicking Int Int


                Assuming you want to stick with just logging the coords for now, you can use this. DO remember that you can destructure in the pattern match if you need.



                update : Msg -> Model -> ( Model, Cmd Msg )
                update msg model =
                case msg of
                NoOp ->
                ( model, Cmd.none )

                Clicking x y ->
                let
                _ =
                Debug.log "coords" ( x, y )
                in
                ( model, Cmd.none )


                You need a Json.Decode.Decoder to that tuple.



                import Json.Decode as Decode exposing (Decoder)


                coordsDecoder : (Int -> Int) -> Decoder a
                coordsDecoder into =
                Decode.field "target" <|
                Decode.map2 into
                (Decode.field "offsetWidth" Decode.int)
                (Decode.field "offsetHeight" Decode.int)


                And lastly, you need the on to take decoder do something with the Event object returned by a JavaScript click event. This decoder gets the coordinates, then Decode.maps the Clicking msg type.



                Html.Events.on "click" (coordsDecoder Clicking)


                With that Decode.field "target" ... you can start decoding whatever you want from the target element.





                Stylistically, you're importing all of the Html.* functions into scope with exposing (..), but you're still prefixing everything which is pretty redundant. You can just use style instead of Html.Attributes.style. Don't be afraid to use as -> Html.Attributes as Attr.






                share|improve this answer

























                  up vote
                  6
                  down vote










                  up vote
                  6
                  down vote









                  So I'm not really sure which of the offsets you're trying to get specifically, but I think this will put you on track. First you need to adjust your Msg ADT's Clicking to have a tuple for the coordinates.



                  type Msg
                  = NoOp
                  | Clicking Int Int


                  Assuming you want to stick with just logging the coords for now, you can use this. DO remember that you can destructure in the pattern match if you need.



                  update : Msg -> Model -> ( Model, Cmd Msg )
                  update msg model =
                  case msg of
                  NoOp ->
                  ( model, Cmd.none )

                  Clicking x y ->
                  let
                  _ =
                  Debug.log "coords" ( x, y )
                  in
                  ( model, Cmd.none )


                  You need a Json.Decode.Decoder to that tuple.



                  import Json.Decode as Decode exposing (Decoder)


                  coordsDecoder : (Int -> Int) -> Decoder a
                  coordsDecoder into =
                  Decode.field "target" <|
                  Decode.map2 into
                  (Decode.field "offsetWidth" Decode.int)
                  (Decode.field "offsetHeight" Decode.int)


                  And lastly, you need the on to take decoder do something with the Event object returned by a JavaScript click event. This decoder gets the coordinates, then Decode.maps the Clicking msg type.



                  Html.Events.on "click" (coordsDecoder Clicking)


                  With that Decode.field "target" ... you can start decoding whatever you want from the target element.





                  Stylistically, you're importing all of the Html.* functions into scope with exposing (..), but you're still prefixing everything which is pretty redundant. You can just use style instead of Html.Attributes.style. Don't be afraid to use as -> Html.Attributes as Attr.






                  share|improve this answer














                  So I'm not really sure which of the offsets you're trying to get specifically, but I think this will put you on track. First you need to adjust your Msg ADT's Clicking to have a tuple for the coordinates.



                  type Msg
                  = NoOp
                  | Clicking Int Int


                  Assuming you want to stick with just logging the coords for now, you can use this. DO remember that you can destructure in the pattern match if you need.



                  update : Msg -> Model -> ( Model, Cmd Msg )
                  update msg model =
                  case msg of
                  NoOp ->
                  ( model, Cmd.none )

                  Clicking x y ->
                  let
                  _ =
                  Debug.log "coords" ( x, y )
                  in
                  ( model, Cmd.none )


                  You need a Json.Decode.Decoder to that tuple.



                  import Json.Decode as Decode exposing (Decoder)


                  coordsDecoder : (Int -> Int) -> Decoder a
                  coordsDecoder into =
                  Decode.field "target" <|
                  Decode.map2 into
                  (Decode.field "offsetWidth" Decode.int)
                  (Decode.field "offsetHeight" Decode.int)


                  And lastly, you need the on to take decoder do something with the Event object returned by a JavaScript click event. This decoder gets the coordinates, then Decode.maps the Clicking msg type.



                  Html.Events.on "click" (coordsDecoder Clicking)


                  With that Decode.field "target" ... you can start decoding whatever you want from the target element.





                  Stylistically, you're importing all of the Html.* functions into scope with exposing (..), but you're still prefixing everything which is pretty redundant. You can just use style instead of Html.Attributes.style. Don't be afraid to use as -> Html.Attributes as Attr.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 16 hours ago

























                  answered Jan 11 '17 at 5:46









                  toastal

                  870614




                  870614
























                      up vote
                      0
                      down vote













                      Excellent answer, in case if i try to get the coordinates in relation to the browser, in case the style left and top are not definity. I am searching some solution and found this






                      offsetParent : a -> Decoder a -> Decoder a
                      offsetParent x decoder =
                      Decode.oneOf
                      [ field "offsetParent" <| Decode.null x
                      , field "offsetParent" decoder
                      ]








                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Excellent answer, in case if i try to get the coordinates in relation to the browser, in case the style left and top are not definity. I am searching some solution and found this






                        offsetParent : a -> Decoder a -> Decoder a
                        offsetParent x decoder =
                        Decode.oneOf
                        [ field "offsetParent" <| Decode.null x
                        , field "offsetParent" decoder
                        ]








                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Excellent answer, in case if i try to get the coordinates in relation to the browser, in case the style left and top are not definity. I am searching some solution and found this






                          offsetParent : a -> Decoder a -> Decoder a
                          offsetParent x decoder =
                          Decode.oneOf
                          [ field "offsetParent" <| Decode.null x
                          , field "offsetParent" decoder
                          ]








                          share|improve this answer












                          Excellent answer, in case if i try to get the coordinates in relation to the browser, in case the style left and top are not definity. I am searching some solution and found this






                          offsetParent : a -> Decoder a -> Decoder a
                          offsetParent x decoder =
                          Decode.oneOf
                          [ field "offsetParent" <| Decode.null x
                          , field "offsetParent" decoder
                          ]








                          offsetParent : a -> Decoder a -> Decoder a
                          offsetParent x decoder =
                          Decode.oneOf
                          [ field "offsetParent" <| Decode.null x
                          , field "offsetParent" decoder
                          ]





                          offsetParent : a -> Decoder a -> Decoder a
                          offsetParent x decoder =
                          Decode.oneOf
                          [ field "offsetParent" <| Decode.null x
                          , field "offsetParent" decoder
                          ]






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 12 '17 at 20:15









                          Luis Angel

                          434




                          434






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41581686%2fi-would-like-know-the-attribute-html-of-an-element-in-elm%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                              SQL update select statement

                              'app-layout' is not a known element: how to share Component with different Modules