There is hundreds of questions like this on forums. Many of them with no answer.
It seems that there is no clear and simple answer. The selected data-id must be stored somewhere, or am I thinking wrong? If it is stored (like the value), how do I retrieve the data?
Can anybody explain how this work or even get me a solution?
“`
<input list=”options” onchange=”val(this)” />
<datalist id=”options”>
<option data-id=”1″>One</option>
<option data-id=”2″>Two</option>
<option data-id=”3″>Three</option>
</datalist>
<input type=”hidden” name=”hidden” id=”idhidden”>
“`
function val(element) {
alert(element.value + ” OK value”);
alert(element.getAttribute(‘data-id’) + ” No value”);
alert(element.selectedIndex + ” No value undefined”);
//this does not work
alert(element.hiddenInput.value.getAttribute(‘data-id’) )
}