html:
<input list="items" id="item">
<datalist id="items">
<option value="A item" data-xyz = "1" >
<option value="aa item" data-xyz = "2" >
<option value="C item" data-xyz = "3" >
<option value="D item" data-xyz = "4" >
<option value="E item" data-xyz = "5" >
</datalist>
<input type="button" id="button" value="Get xyz" />
js:
$("#button").click(function(){
alert($("#items option:selected").attr('data-xyz'));
});
Query:
I just need to access the value of ‘data-xyz’ from selected datalist
option on click of “#button” or any event.jQuery version: 1.7.2
Thanks.
datalistis simply a storage for autocomplete. Since it could be used for multiple elemnts in page, it wil not have a selected property.You will need to locate the applicable option yourself such as the following
DEMO: http://jsfiddle.net/shcRJ/