3

I'd like to use something like this snippet

<input type="text" name="product" list="productName"/>
<datalist id="productName">
    <option value="Pen">Pen</option>
    <option value="Pencil">Pencil</option>
    <option value="Paper">Paper</option>
</datalist>

But I need to run some js code whenever the selection is changed (or the value edited). Is there a way to hook up an event handler for that?

Community
  • 1
  • 1
John La Rooy
  • 295,403
  • 53
  • 369
  • 502

1 Answers1

4

The input event would work. The change event works as well, but it is only triggered when the input element looses focus, not every time the value changes.

document.querySelector('input').oninput = function() {
    console.log(this.value);
};

DEMO

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143