yysun/apprun

How to get new value of 'input' when not using JSX/compiler?

Closed this issue · 7 comments

In my toy AppRun project I still try to avoid using any compiler or build tool, just everything in a single file. However, now I need to obtain the new value of an <input type="text" ...> element, and I'm not sure if it can be done by including an oninput attribute like the one below, since this will not refer to the input element:

oninput="app.run('input_evt', ...this.value...)"

Is there a simple solution to this?

Maybe I have to use a plain JavaScript myInput.addEventListener('input', ...), but I'm not sure what's the right time/place to do that. I must of course be sure that myInput is referring to the right input element when the event happens.

I found a solution, and it did not make use of addEventListener. For each input element, I assigned a unique id, an id that also told where in my state/model the new value should go. In the oninput="app.run(...)" of the input element I included the id as a parameter, and in the update handler for that event I could do a document.getElementById(id).value to get the new value. Then I just had to decode the id to find where to put the value.

yysun commented

The way I usually do is to use the event keyword:

oninput="app.run('input_evt', event)"

You can get the value out of the event target:

update = {
  'input_evt': (state, e) => state = e.target.value
}

Thanks. According to MDN: window.event is a proprietary Microsoft Internet Explorer property.
However, it seems to be widely supported.

yysun commented

You can do this too:

oninput="app.run('input_evt', this)"
update = {
  'input_evt': (state, el) => state = el.value
}

Of course. Why didn't I think of that? ;-)