# After update, previous onClick msg sent automatically

**URL:** https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619
**Category:** Learn
**Created:** [November 25, 2018, 11:45pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619 "2018-11-25T23:45:35Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)
#### Post date: [November 25, 2018, 11:45pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/1 "2018-11-25T23:45:35Z")

</div>

After sending a message to update the model a second message is automatically being sent to keep the menus that were the source of the first message open instead of allowing them to close as intended. Please see the current state of affairs [here](https://stevensonmt.github.io/listoPF/) and the source code [here](https://github.com/stevensonmt/listoPF).

Note that I have edited this question to reflect a better understanding of the problem that was not apparent when I originally posted.

---

<div class="post-metadata">

### Author: ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)
#### Post date: [November 26, 2018, 5:13am UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/2 "2018-11-26T05:13:29Z")

</div>

It would help a lot if you could give full working example in ellie.

---

<div class="post-metadata">

### Author: ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)
#### Post date: [November 26, 2018, 9:47am UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/3 "2018-11-26T09:47:26Z")

</div>

Try adding some debug statements to see what is happening with the model.

You could do it like this:

Rename `update` to `innerUpdate`, then

```
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model = Debug.log "updateResult" (innerUpdate (Debug.log "msg" msg) (Debug.log "model" model)

```

And read the console output to check if the model is really changed or not, and then drill down from there with more debug statements until you figure it out.

You could also use the time travel debugger, but its UI is sometimes awkward. Debug statements are more precise as you can target the problem area more exactly.

---

<div class="post-metadata">

### Author: ![hpate](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/hpate/32/2094_2.png) [@hpate](https://discourse.elm-lang.org/u/hpate)
#### Post date: [November 26, 2018, 2:44pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/4 "2018-11-26T14:44:39Z")

</div>

In `AddNewToDo` it gets the current time and then goes through with `GetID <time>`, but in `GetID` it doesn’t insert the new todo into the `Dict`.

---

<div class="post-metadata">

### Author: ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)
#### Post date: [November 26, 2018, 3:33pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/5 "2018-11-26T15:33:30Z")

</div>

[https://ellie-app.com/3ZnrfqqWdDfa1](https://ellie-app.com/3ZnrfqqWdDfa1)

I apologize for the icons. I just grabbed links to things that were roughly the right size as I did not have time to upload the actual icons to a shareable site. The red icon on the bottom opens the main menu. The far left icon on that menu adds an item. Once an item is added clicking the item opens the item menu. The top menu choice there is to edit the item. The middle is to mark it complete and the last to delete it. The complete and delete actions should result in the item menu closing and the view re-rendering, but it does not.

---

<div class="post-metadata">

### Author: ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)
#### Post date: [November 26, 2018, 3:34pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/6 "2018-11-26T15:34:55Z")

</div>

> [@hpate](#):
>
> In `AddNewToDo` it gets the current time and then goes through with `GetID <time>` , but in `GetID` it doesn’t insert the new todo into the `Dict` .

This is intentional. If you grab a new ID but don’t submit a name/description then the Dict should not be updated. This is why GetID changes the view to the Editing screen.

---

<div class="post-metadata">

### Author: ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)
#### Post date: [November 26, 2018, 3:54pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/7 "2018-11-26T15:54:46Z")

</div>

> [@stevensonmt](#):
>
> The complete and delete actions should result in the item menu closing and the view re-rendering, but it does not.

I tested this a bit, and both of those does seem to cause model to update and view to re-render, BUT do not cause menu to close, until you move mouse out of the menu item. So this seems to be a bug of menu not closing, and not of model not updating.

I don’t know elm-ui so I have no idea why menu isn’t closing here.

---

<div class="post-metadata">

### Author: ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)
#### Post date: [November 26, 2018, 4:08pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/8 "2018-11-26T16:08:09Z")

</div>

The model isn’t updating correctly. The Complete and Delete update functions should set `model.openMenu` to `None`, but it remains at `ItemMenu`. Using the Debugger on Ellie it appears that the `LaunchItemMenu` message is being sent after the `UpdateToDo` message completes. But I can’t figure out why.

---

<div class="post-metadata">

### Author: ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)
#### Post date: [November 26, 2018, 5:33pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/9 "2018-11-26T17:33:06Z")

</div>

> [@stevensonmt](#):
>
> The model isn’t updating correctly. The Complete and Delete update functions should set `model.openMenu` to `None` , but it remains at `ItemMenu` . Using the Debugger on Ellie it appears that the `LaunchItemMenu` message is being sent after the `UpdateToDo` message completes. But I can’t figure out why.

I think I figured this out. I tested clicking the middle menu option to complete the task. Messages in debug are:

1. LaunchItemMenu ; model.CurrentToDoID = Just …
2. CompleteToDo ; model.CurrentToDoID = Nothing
3. LaunchItemMenu ; model.CurrentToDoID = Just …

So with `CompleteToDo` you change `model.CurrentToDoID`, which then changes `selected` value of your radio button (see code below), triggering its `onChange` event of `LaunchItemMenu`:

```auto
listTasksView : Model -> Element Msg
listTasksView model =
    column []
        [Input.radio [ Font.size 24, Font.color (Element.rgb255 200 200 200)]
            { onChange = \id -> LaunchItemMenu id
            , selected = model.currentToDoID --Nothing
            , label = Input.labelAbove [] (text "")
            , options =

```

---

<div class="post-metadata">

### Author: ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)
#### Post date: [November 26, 2018, 5:39pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/10 "2018-11-26T17:39:27Z")

</div>

That makes total sense now. THANK YOU! This has been driving me crazy. I now just have to figure out a way around that.

---

<div class="post-metadata">

### Author: ![Jess\_Bromley](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jess_bromley/32/3396_2.png) [@Jess\_Bromley](https://discourse.elm-lang.org/u/Jess_Bromley)
#### Post date: [November 26, 2018, 8:05pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/11 "2018-11-26T20:05:20Z")

</div>

I think it should be considered a bug in _elm-ui_. Maybe try filing a bug report?

When you use the core _elm/html_ library you do not expect to trigger the messages assigned to `onChange`, `onChecked` or `onInput` if you internally modify the value of an element (such as when dynamically removing disallowed characters typed into a text input).

A quick check confirms this is true in the specific case of radio buttons:  
[https://ellie-app.com/3Zsv5FjD8NWa1](https://ellie-app.com/3Zsv5FjD8NWa1)  
Note how the message counts don’t increase when the values of the radio buttons are altered by internal logic rather than by clicking on them directly. (Note that if I was writing something like this for real I’d not rely on my internal logic but rather a union type to stop more than one radio being selected, I was just coding up something quickly that would show individual message counts for each radio).

---

<div class="post-metadata">

### Author: ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)
#### Post date: [November 26, 2018, 11:28pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/12 "2018-11-26T23:28:37Z")

</div>

@mdgriffith: Do you agree this is a potential elm-ui bug? If so I can set up an issue on github.

---

<div class="post-metadata">

### Author: ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)
#### Post date: [November 27, 2018, 2:16am UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/13 "2018-11-27T02:16:37Z")

</div>

I don’t think that’s it after all. I just changed from radio buttons to a list of Input.buttons and the same behavior persists. The buttons do not have an intrinsic `onChange` so something else is triggering it. The root issue is something triggers the `model.currentToDoID` to revert back to the previously selected item ID. This leads to the item menu staying open.  
Ellie without radio buttons below:

[https://ellie-app.com/3ZzbLSCHY2Ma1](https://ellie-app.com/3ZzbLSCHY2Ma1)

I have edited the original post in this thread to reflect my new understanding of the problem with link to the full source code on github and the current version compiled with --debug.

> **Disclaimer from embarassment**
>
> I am aware that some code is redundant and other code is “dead” because I was in the middle of changing my layout and approach when I uncovered this problem. I plan to do some heavy refactoring once the bugs are cleared up.

---

<div class="post-metadata">

### Author: ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)
#### Post date: [November 27, 2018, 10:25am UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/14 "2018-11-27T10:25:38Z")

</div>

The way I debug this is to first change `LaunchItemMenu` message from `LaunchItemMenu Int` to `LaunchItemMenu Int String`.

Then I add different string to each place where that message can be generated, and in `update` change `LaunchItemMenu id ->` to `LaunchItemMenu id _ ->` so that extra string is just ignored. (It’s just for debug output so I’ll know the exact location where problematic message is generated.)

In this case problem is line 310:

```auto
listTasksView model =
...
                Input.button
...

                    { onPress = Just (LaunchItemMenu id "310")
                    , label = Element.text (task.name)
                    }
...

```

According to documentation of [button](https://package.elm-lang.org/packages/mdgriffith/elm-ui/latest/Element-Input#button), `onPress` can be fired at Enter-key press or `onClick`. I’m not pressing any keys here so it must be `onClick`. Only click I’m doing is clicking the item at ItemMenu. But as it happens, you are creating ItemMenu as a child element of the button (see code below), so when clicking any item in ItemMenu, that event will propagate also to button and fire its `onPress` handler.

```auto
listTasksView model =
...
                Input.button
...
                    , Element.onLeft
                        (if Just id == model.currentToDoID then
                            (launchItemMenu model)
                         else
                            Element.none
                        )
...

```

Possible fix might be to prevent that event propagation, but I don’t know how to do that in elm-ui, or whether that is even possible.

---

<div class="post-metadata">

### Author: ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)
#### Post date: [November 27, 2018, 10:54am UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/15 "2018-11-27T10:54:46Z")

</div>

Actually just removing the `onPress` event handler while ItemMenu is being shown seems to work:  
[https://ellie-app.com/3ZJVjNF5LbKa1](https://ellie-app.com/3ZJVjNF5LbKa1)

```auto
                    { onPress =
                        (if Just id == model.currentToDoID then
                            Nothing
                         else
                            Just (LaunchItemMenu id)
                        )                    

```

Going back to your earlier version with radio button, unfortunately `onChange` of radio button doesn’t accept `Maybe msg` like `onPress` does, but you can just generate message there that is ignored:  
[https://ellie-app.com/3ZK3CSNGjCRa1](https://ellie-app.com/3ZK3CSNGjCRa1)

```auto
listTasksView : Model -> Element Msg
listTasksView model =
    column []
        [Input.radio [ Font.size 24, Font.color (Element.rgb255 200 200 200)]
            { onChange =
                if model.currentToDoID == Nothing then
                    \id -> LaunchItemMenu id
                else
                    \_ -> Ignored
...
type Msg
    = AddNewToDo
...
    | Ignored

update msg model =
    case msg of
        Ignored ->
            ( model, Cmd.none )
...

```

---

<div class="post-metadata">

### Author: ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)
#### Post date: [November 27, 2018, 3:00pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/16 "2018-11-27T15:00:16Z")

</div>

Great explanation and solution. Hadn’t occurred to me that children of buttons would activate the button, but of course they should. A quirk that could actually lend itself to interesting design choices for UI at some point. Thanks again!

---

<div class="post-metadata">

### Author: ![mdgriffith](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mdgriffith/32/61_2.png) [@mdgriffith](https://discourse.elm-lang.org/u/mdgriffith)
#### Post date: [December 1, 2018, 4:58pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/17 "2018-12-01T16:58:12Z")

</div>

Hey! Sorry, I haven’t been following, but am definitely interested. If someone wants to put together a brief summary of what you all found and an SSCCEE in an issue on the repo, I’d definitely appreciate it 😃

I’m not sure if there’s a direct solution elm-ui can do, but at the very least I can include a note about it in the guide.

---

<div class="post-metadata">

### Author: ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)
#### Post date: [December 1, 2018, 6:55pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/18 "2018-12-01T18:55:44Z")

</div>

I don’t think it’s an em ui issue directly after all. If anything it might be considered an elm bug because one event propagates two Msgs somehow.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)
#### Post date: [December 11, 2018, 6:55pm UTC](https://discourse.elm-lang.org/t/after-update-previous-onclick-msg-sent-automatically/2619/19 "2018-12-11T18:55:50Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
