Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to execute some code when an item from an HTML5 datalist has been selected?

For example:

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

Is it possible to catch the event when an item from the datalist had been selected? Calling onclick or onchange on the input element don't work.

like image 612
3Nex Avatar asked Nov 12 '22 11:11

3Nex


1 Answers

    $(document).ready(function() {

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

}); 
});
like image 90
Neel Avatar answered Nov 15 '22 13:11

Neel