Home > Software design >  get data-attribute of selection datalist item
get data-attribute of selection datalist item

Time:10-04

I have this datalist:

<input list="kundenliste" id="inputKundenliste">
<datalist id="kundenliste">
   <option data-info="MyInfo1">ABC</option>
   <option data-info="MyInfo2">CDE</option>
</datalist>

I would like to get the data-info attribute of the selected option with jQuery. How can I get this ? I tried this:

$("#inputKundenliste").change(function() {
   console.log($(this).data());
});

Output:

{ }

CodePudding user response:

May this code work:

$("#inputKundenliste").change(function() {
var elementValue = $(this).val();
  console.log($('#kundenliste').find(`option:contains(${elementValue})`).data());
});
  • Related