How to get data attribute value for selected option

This post shows you How to get data attribute value for selected option using jQuery.

For example

<select id="ddlProduct" name="ProductId">
    <option value="1" data-name="phone">IPhone</option>
    <option value="2" data-name="phone">Samsung</option>
</select>

jquery data attribute

To get data attribute value you can write your code as shown below.

$('#ddlProduct').change(function () {
    var data = $("#ddlProduct option:selected").data("name");
});

As you can see, you can easily using jquery get data attribute value of selected option.