On select change, get data attribute value

Jquery

Jquery Problem Overview


The following code returns 'undefined'...

$('select').change(function(){
    alert($(this).data('id'));
});

<select>
    <option data-id="1">one</option>
    <option data-id="2">two</option>
    <option data-id="3">three</option>
</select>

Jquery Solutions


Solution 1 - Jquery

You need to find the selected option:

$(this).find(':selected').data('id')

or

$(this).find(':selected').attr('data-id')

although the first method is preferred.

Solution 2 - Jquery

Try the following:

$('select').change(function(){
  alert($(this).children('option:selected').data('id'));
});

Your change subscriber subscribes to the change event of the select, so the this parameter is the select element. You need to find the selected child to get the data-id from.

Solution 3 - Jquery

document.querySelector('select').onchange = function(){   
   alert(this.selectedOptions[0].getAttribute('data-attr')); 
};

Solution 4 - Jquery

Vanilla Javascript:

this.querySelector(':checked').getAttribute('data-id')

Solution 5 - Jquery

You can use context syntax with this or $(this). This is the same effect as find().

$('select').change(function() {
    console.log('Clicked option value => ' + $(this).val());
    <!-- undefined console.log('$(this) without explicit :select => ' + $(this).data('id')); -->
    <!-- error console.log('this without explicit :select => ' + this.data('id')); -->
    console.log(':select & $(this) =>    ' + $(':selected', $(this)).data('id'));
    console.log(':select & this =>       ' + $(':selected', this).data('id'));
    console.log('option:select & this => ' + $('option:selected', this).data('id'));
    console.log('$(this) & find =>       ' + $(this).find(':selected').data('id'));
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
    <option data-id="1">one</option>
    <option data-id="2">two</option>
    <option data-id="3">three</option>
</select>

As a matter of microoptimization, you might opt for find(). If you are more of a code golfer, the context syntax is more brief. It comes down to coding style basically.

Here is a relevant performance comparison.

Solution 6 - Jquery

$('#foo option:selected').data('id');

Solution 7 - Jquery

Maybe a more elegant way

$('option:selected', this).data('id')

Solution 8 - Jquery

this works for me

<select class="form-control" id="foo">
    <option value="first" data-id="1">first</option>
    <option value="second" data-id="2">second</option>
</select>

and the script

$('#foo').on("change",function(){
    var dataid = $("#foo option:selected").attr('data-id');
    alert(dataid)
});

Solution 9 - Jquery

By using this you can get the text, value and data attribute.

<select name="your_name" id="your_id" onchange="getSelectedDataAttribute(this)">
    <option value="1" data-id="123">One</option>
    <option value="2" data-id="234">Two</option>
</select>

function getSelectedDataAttribute(event) {
    var selected_text = event.options[event.selectedIndex].innerHTML;
    var selected_value = event.value;
    var data-id = event.options[event.selectedIndex].dataset.id);    
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionuserBGView Question on Stackoverflow
Solution 1 - JqueryJordan BrownView Answer on Stackoverflow
Solution 2 - JqueryRich O'KellyView Answer on Stackoverflow
Solution 3 - JquerySergey KovalenkoView Answer on Stackoverflow
Solution 4 - JqueryArthur RonconiView Answer on Stackoverflow
Solution 5 - JquerymickmackusaView Answer on Stackoverflow
Solution 6 - JquerySinan ÇALIŞKANView Answer on Stackoverflow
Solution 7 - JqueryJJaunView Answer on Stackoverflow
Solution 8 - JqueryJohn Bryan CallejaView Answer on Stackoverflow
Solution 9 - JqueryAbhishek GuptaView Answer on Stackoverflow