How to get attribute from selected option using jQuery

0

The jQuery :selected selector can be used in conjugation with the val() method to find the selected option value in a select box or dropdown list. This tutorial teach you how to get attribute from selected option using jQuery.

HTML Code

<select id="language" >
    <option data-val="laravel" value="0">Laravel</option>
    <option data-val="php" value="1">PHP</option>
    <option data-val="ajax" value="2">Ajax</option>
</select>

jQuery Code

var data_val = $('#language option:selected').attr('data-val');
// Selected option attribute
var val = $('#language').val();
// Selected option value