I'm trying to find a value of data-id from selected option in data list. I have a function which returns correct value but also is giving me a popup with 'NaN'. As a standard response to my problem I'm using 'intParse' or 'isNaN' function but nothing seems to stop returning 'NaN'
So I got two alerts . First with the correct value, and second with 'NaN' when calling this function.
$("#EducationEstablishment").on('input', function() {
var id = GetDataListOptionValue(EducationEstablishment, establishments);
alert(id)
});
function GetDataListOptionValue(datalistInputId, dataListId) {
var x = $(datalistInputId).val();
var z = $(dataListId);
var val = $(z).find('option[value="' + x + '"]');
endval = val.attr('data-id');
var num = isNaN(parseInt(endval)) ? 0 : parseInt(endval)
return (num);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="EducationEstablishment" list="establishments" type="text">
<datalist id="establishments">
<option data-id="100018962" value="Instituto Tecnologico De Buenos Aires"></option>
<option data-id="100084386" value="National University of Cordoba, Argentina"></option>
</datalist>