# Simulate an onInput event from javascript

**URL:** <https://discourse.elm-lang.org/t/simulate-an-oninput-event-from-javascript/3645>\
**Category:** Learn\
**Created:** [May 15, 2019, 12:19pm UTC](https://discourse.elm-lang.org/t/simulate-an-oninput-event-from-javascript/3645 "2019-05-15T12:19:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chalmagean](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/chalmagean/32/3785_2.png) [@chalmagean](https://discourse.elm-lang.org/u/chalmagean)\
**Post date:** [May 15, 2019, 12:19pm UTC](https://discourse.elm-lang.org/t/simulate-an-oninput-event-from-javascript/3645/1 "2019-05-15T12:19:28Z")

</div>

I’m testing an Elm form with selenium and whenever I change the value of a select, Elm doesn’t receive the event. So I am unable to simulate a real user selecting a different option in a dropdown.

I’ve tried triggering the event with javascript in the browser with no luck.

What’s the best way to do this?

---

<div class="post-metadata">

**Author:** ![norpan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/norpan/32/548_2.png) [@norpan](https://discourse.elm-lang.org/u/norpan)\
**Post date:** [May 15, 2019, 5:44pm UTC](https://discourse.elm-lang.org/t/simulate-an-oninput-event-from-javascript/3645/2 "2019-05-15T17:44:11Z")

</div>

The event for select is `onchange`. Try that!

---

<div class="post-metadata">

**Author:** ![chalmagean](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/chalmagean/32/3785_2.png) [@chalmagean](https://discourse.elm-lang.org/u/chalmagean)\
**Post date:** [May 16, 2019, 6:32am UTC](https://discourse.elm-lang.org/t/simulate-an-oninput-event-from-javascript/3645/3 "2019-05-16T06:32:52Z")

</div>

So you mean something like the following?

`$("#country").val("nl").trigger("onchange")`

That doesn’t work. Meaning Elm doesn’t receive the event.

---

<div class="post-metadata">

**Author:** ![chalmagean](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/chalmagean/32/3785_2.png) [@chalmagean](https://discourse.elm-lang.org/u/chalmagean)\
**Post date:** [May 16, 2019, 7:24am UTC](https://discourse.elm-lang.org/t/simulate-an-oninput-event-from-javascript/3645/4 "2019-05-16T07:24:22Z")

</div>

I figured it out. Here’s the code snippet that did it.

```
  var event = new Event('input', { 'bubbles': true, 'cancelable': true });
  var el = document.getElementById("country");
  el.dispatchEvent(event);
```

---

<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:** [May 26, 2019, 7:24am UTC](https://discourse.elm-lang.org/t/simulate-an-oninput-event-from-javascript/3645/5 "2019-05-26T07:24:23Z")

</div>

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