0

HTML:

<div class="form-group ">
   <select class="form-control input-sm col-xs-2 game-select" data-game="LuckyTuesday">
        <option value="" disabled selected>Select Game </option>

        <option id="1" data-name="Direct 1" data-minNumber="1" data-maxNumber="1">
            Direct 1
        </option>
        <option id="2" data-name="Direct 2" data-minNumber="2" data-maxNumber="2">
            Direct 2
        </option>
   </select>
</div>

JavaScript:

$('select').change(function () {
    let gameType = $(this).find(':selected').attr('data-name');

    //How to can I use "game-select" class to get "data-game" att? 
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • You know how to get the value of the option, isn't it the same for the `select` itself? – Barmar Dec 13 '19 at 23:45
  • Does this answer your question? [jQuery get value of select onChange](https://stackoverflow.com/questions/11179406/jquery-get-value-of-select-onchange) – naeramarth7 Dec 13 '19 at 23:47
  • @naeramarth7 The question isn't about the value, it's about a `data-XXX` attribute. – Barmar Dec 13 '19 at 23:47

1 Answers1

2

You can get the value of data-game simply by using the data() jQuery function:

$(this).data('game');
Martin Heralecký
  • 5,649
  • 3
  • 27
  • 65