Christian Hausknecht
08/13/2021, 3:49 PMinput
element with a datalist
attached as kind of "type ahead" component. In order to add some logic within an UI framework (I work on the fritz2 framework FYI), one need to register a listener on specific events of course. For this use case I consider the input
event as the best fitting one. According to MDN the event has the interface InputEvent
.
But this seems to be false for some circumstances and browsers: If one selects a proposal from the datalist at least in Chrome and Safari Browsers (mobile too) instead a simple Event
event is fired!
Here is a small HTML file to make this visible:
<html>
<body>
<input id="myinput" type="text" list="mydata">
<datalist id="mydata">
<option value="Kotlin"></option>
<option value="Scala"></option>
<option value="Java"></option>
<option value="Python"></option>
<option value="Commodore Basic V2"></option>
</datalist>
<script>
document.getElementById("myinput").oninput = function(e) {
console.log(Object.getPrototypeOf(e))
}
</script>
</body>
</html>
In Firefox one always get the InputEvent
no matter if just typing a letter or selecting a proposal from the list. In Chrome or Safari this differs; when selecting from the list, only Event
is fired.
This leads to problems in Kotlin of course if one tries to cast this depending on the event name, as input
does not always return the same type as shown above.
How to deal with this situation? Is there some kind of specification that reveals the different events for the different browsers? What's the reason for browsers to offer different event types for the same event key?
(If someone has interest in the framework specific impacts, have a look at the appropriate PR on github, which will lead to the relevant code sections.)Christian Hausknecht
08/19/2021, 7:07 AMclose
or error
for example) might freely decide which event interface they use... not pleasent for static typed languages! (At least without real sum types!!! We need those in Kotlin 😉)Ilya Kalibrov [JB]
08/19/2021, 8:36 AMChristian Hausknecht
08/19/2021, 9:02 AM