Is it possible to execute some code when an element is selected from an HTML5 datalist?

For instance:

<input type="text" list="sample"/>
<datalist id="sample">
    <option value="item 1"/>
    <option value="item 2"/>
</datalist>

Is it possible to catch an event when an item was selected from the data catalog? Calling onclick or onchange on an input element does not work.

+5
source share
2 answers
    $(document).ready(function() {

$("#search").on("input", function(e) {
var val = $(this).val();
if(val === "") return;
....
    var dataList = $("#searchresults");
dataList.empty();
 your code...

}); 
});
+1
source

Datalistused to storage and Auto list Purpose... It doesn't have Selected Events..

if you want to get elements in a datalist ... just loop it and get the data ...

$('#sample option').each(function(index) {
 alert($(this).val());
});
0
source

All Articles