I'm working on kind of an autocomplete-like feature, and instead of rendering my own dropdown, I'm trying out < datalist > because I like using native elements for use on different devices, and also it already comes with a UI feature of selecting items using arrows and enter key.
However when it comes to executing JS events it doesn't seem to offer much.
Lets say I have datalist options like this:
<datalist>
<option value="item_name" data-id="item_id">
<option value="item_name" data-id="item_id">
<option value="item_name" data-id="item_id">
...
</datalist>
I need to show the item_name in the list, but when an item is picked, I want to execute something that will use the item_id of that item (send an ajax request for example). But there doesn't seem to be a way to access option's attributes from the < input >'s events.
Is there a way to do this, or can you recommend a different type of native element that could do this, but also be able to go through items using arrow keys and select with enter?
Thanks
I tried using change and input events on the < input > field but they don't seem to have data of the selected option field, and the < datalist > element itself doesn't seem to have any JS events.
I was hoping to be able to trigger something like a change event when I pick an < option >, like you do with < select > elements.