Within this particular scenario, there is an input field paired with a corresponding datalist element. My aim is to develop JavaScript code that actively listens for when a user chooses an item from the list. Most resources suggest utilizing the "input" event handler for this task, and in browsers like Chrome and Firefox it functions seamlessly. However, Internet Explorer presents a challenge.
In IE10, I have observed the following behavior:
- Tapping into the field triggers the event.
- The event does not trigger upon initial selection of an item from the datalist.
- Re-selecting the same option successfully fires the event.
To demonstrate, here's a test:
https://i.stack.imgur.com/f7Eqn.gif
$('input').on('input', function(){
console.log($('input').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" />
<datalist id="ice-cream-flavors">
<option value="Chocolate">
<option value="Coconut">
<option value="Mint">
<option value="Strawberry">
<option value="Vanilla">
</datalist>
If anyone has any insights on how I can compel Internet Explorer to trigger the desired event (or any event) so that I may execute a function upon user selection, I would greatly appreciate it.