Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event handler for input+datalist

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?

like image 690
John La Rooy Avatar asked Apr 15 '26 04:04

John La Rooy


1 Answers

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

like image 51
Felix Kling Avatar answered Apr 16 '26 19:04

Felix Kling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!