I want to get the data Attribute of a select Option but it does not work
html
<select class="form-control" id="device-selection" style="width:300px;">
<option selected="selected">Choose!</option>
<option data-width="1080" data-height="1920">Huewai p9</option>
</select>
JS:
$('#device-selection').onchange = function() {
console.log($('#device-selection').data('width'));
console.log($('#device-selection').data('height'));
}
I Keep getting this error:
Uncaught TypeError: Cannot set property 'onchange' of null
My Jquery Version is 3.1.1
data attris in option selector should be$('#device-selection option:selected')$()is returningnull. You need to make sure your own code runs after jQuery has been loaded. How and where are you including jQuery? You need to post your entire code.select, but the data width and height is attr of the selected option